gadzik
gadzik

Reputation: 47

AngularJS controller dont work after adding a service as dependency

hello i'm working on my small project using angular , during the creating my second controller with second service i have a problem , when i inject my own service as a dependency in the controller it dont work and all of expressions in html file are not resolved (before adding a service all works) here is my code

after adding AccSrv to AccCtrl the problem occurs

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;
        })

    }
}]);

Upvotes: 1

Views: 82

Answers (2)

BShaps
BShaps

Reputation: 1414

@yBrodsky is correct you need to change

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

To:

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

Upvotes: 0

yBrodsky
yBrodsky

Reputation: 5041

The problem is that your service is being defined in a module that you are not including anywhere:

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

should be

myApp.service('AccSrv'...etc

Otherwise you will need to include the AccountService MODULE as a dependency of myApp.

Upvotes: 3

Related Questions