2017-04-12 20 views
1

你好我正在使用角度我的小項目,在創建我的第二個控制器與第二個服務期間我有一個問題,當我注入自己的服務作爲依賴控制器將它添加AccSrv到AccCtrl問題後這裏是我的代碼AngularJS控制器不會工作後添加服務作爲依賴項

(添加服務的所有作品前)不工作和所有的HTML文件中表達的都沒有解決發生

angular.module('myApp', ['ngRoute', 'membersService']).config(
 
    ['$httpProvider', '$routeProvider', function ($httpProvider, $routeProvider) { 
 
     $routeProvider.when('/home', { 
 
      templateUrl: 'partials/home.html', 
 
      controller: 'MembersCtrl' 
 
     }).when('/account', { 
 
      templateUrl: 'partials/account.html', 
 
      controller: 'AccCtrl' 
 
     }).otherwise({ 
 
      redirectTo: '/home' 
 
     }); 
 
    }]);


<!DOCTYPE html> 
 
<html lang="en" ng-app="myApp"> 
 
<head> 
 
    <title>myApp</title> 
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
 
    <meta name="format-detection" content="telephone=no"/> 
 
    <link rel="stylesheet" href="css/screen.css" type="text/css"/> 
 
    <script src="js/libs/angular.js"></script> 
 
    <script src="js/libs/angular-route.js"></script> 
 
    <script src="js/libs/angular-resource.js"></script> 
 
    <script src="js/app.js"></script> 
 
    <script src="js/services/MemberSrv.js"></script> 
 
    <script src="js/controllers/MemberCtrl.js"></script> 
 
    <script src="js/services/AccSrv.js"></script> 
 
    <script src="js/controllers/AccCtrl.js"></script> 
 

 
</head> 
 
<body> 
 
<div ng-view></div> 
 
</body> 
 
</html>


<!DOCTYPE html> 
 
<html lang="en" ng-app="myApp" ng-controller="AccCtrl"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Title</title> 
 
</head> 
 
<body> 
 
<p>{{hello}}</p> 
 
<p>{{world}}</p> 
 
<form name="accForm" ng-submit="createAccount()"> 
 
    <div> 
 
     <label for="email">Email</label> 
 
     <input type="text" name="email" id="email" ng-bind="newAccount.email"/> 
 
    </div> 
 
    <div> 
 
     <label for="password">Password</label> 
 
     <input type="password" name="password" id="password" ng-bind="newAccount.password"/> 
 
    </div> 
 
    <div> 
 
     <input type="submit" id="registerAccount" value="Sign-up"/> 
 
    </div> 
 
</form> 
 

 
<form name="accForm" ng-submit="getAccountByEmail()"> 
 
    <div> 
 
     <label for="getemail">CheckEmail</label> 
 
     <input type="text" name="getemail" id="getemail" ng-bind="ExistingAccount.email"/> 
 
    </div> 
 

 
    <div> 
 
     <input type="submit" id="check" value="Check"/> 
 
    </div> 
 
</form> 
 
<table> 
 
    <tr> 
 
     <td>{{ExistingAccount.id}}</td> 
 
     <td>{{ExistingAccount.email}}</td> 
 
     <td>{{ExistingAccount.password}}</td> 
 
     <td>{{ExistingAccount.creationDate}}</td> 
 
    </tr> 
 
</table> 
 

 
</body> 
 
</html>


angular.module('AccountService', []).service('AccSrv', ['$http', function ($http) { 
 

 
    this.getAccountByEmail = function (email) { 
 
     var req = { 
 
      method: 'GET', 
 
      url: "http://localhost:8080/gadziksy-web/rest/account/" + email 
 
     }; 
 
     return $http(req); 
 
    }; 
 
}]);


var myApp = angular.module('myApp'); 
 
myApp.controller('AccCtrl',['$scope','$http' ,'AccSrv' , function ($scope , $http , AccSrv) { 
 
    $scope.hello = 'hello'; 
 
    $scope.world = 'world'; 
 
    $scope.ExistingAccount = { 
 
     "id": '', 
 
     "email": '', 
 
     "password": '', 
 
     "creationDate": '' 
 
    }; 
 

 
    $scope.getAccount = function() { 
 
     return AccSrv.getAccountByEmail($scope.ExistingAccount.email).then(function (data) { 
 
      $scope.ExistingAccount = data.data; 
 
     }) 
 

 
    } 
 
}]);

+0

您可以提供訪問加載該控制器的頁面時收到的錯誤消息嗎?要查看錯誤消息,請右鍵單擊並單擊檢查(如果您使用的是Chrome) – BShaps

+1

您需要將AccountService模塊添加到您的應用程序模塊中。您的服務是在不同於'myApp'的模塊中定義的。這裏是另一個鏈接,它解釋它'http:// stackoverflow.com/questions/22966858/angularjs-access-service-from-different-module' – Knitesh

+0

謝謝你的回答這很有幫助 – gadzik

回答

3

的問題是,你的服務,你是不是任何地方,包括在一個模塊中被定義爲:

angular.module('AccountService', []).service('AccSrv'...etc 

應該

myApp.service('AccSrv'...etc 

否則,您將需要包含AccountService MODULE作爲myApp的依賴項。

+0

你節省了我的時間謝謝你 – gadzik

0

@yBrodsky是正確的,你需要改變

angular.module('AccountService', []).service('AccSrv', ['$http', function ($http) { 

要:

angular.module('myApp').service('AccSrv', ['$http', function ($http) { 
0

AccSrv從一個不同的模塊(AccountService)被declarated聲明AccCtrlmyApp)。

忽略代碼

//... 
angular.module('AccountService', []).service('AccSrv', ... 
//... 


//... 
var myApp = angular.module('myApp'); 
myApp.controller('AccCtrl', ... 
//... 

這樣,當你嘗試在模塊myApp定位AccSrv,它不能被發現

....除非你在myApp模塊聲明AccountService作爲一個依賴這樣的:

//... 
angular.module('myApp', ['AccountService']) 
//... 

嘗試在模塊AccountService或反之亦然聲明AccCtrl(或添加依賴如前所述)。

相關問題