0

I am learning angular.js from the website below, http://docs.angularjs.org/guide/dev_guide.services.injecting_controllers

I tried to run the snippet in my local server. But it failed. I thought the issue was that the controller did not know the 'notify' factory. Could someone help to correct the codes?Thanks.

html

<body ng-app="myApp">
 <div id="simple" ng-controller="myController">
    <p>Let's try this simple notify service, injected into the controller...</p>
    <input ng-init="message='test'" ng-model="message" >
    <button ng-click="callNotify(message);">NOTIFY</button>
    <p>(you have to click 3 times to see an alert)</p>
</div>

JS

 <script>
//MyServiceModule service module
angular.module('MyServiceModule', []).factory('notify', ['$window', function(win) {
    var msgs = [];
    return function(msg) {
    msgs.push(msg);
    if (msgs.length == 3) {
    win.alert(msgs.join("\n"));
    msgs = [];
    }
    };
}]);
//Controller
function myController(scope, notifyService) {
    scope.callNotify = function(msg) {
    notifyService(msg);
    };
}
myController.$inject = ['$scope','notify'];
// How to let the myController know the 'notify' factory in MyServiceModule?

//myApp
angular.module('myApp', [])
.controller('myController',myController);

</script>

1 Answer 1

2

Your app must declare dependcie for the other module like this:

//myApp
angular.module('myApp', ["MyServiceModule"])
.controller('myController',myController);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.