angularjs - How to inject an service into controller by angular.js? -


I am learning angular.js from the website below,

I try to run the snippet in my 'S local server but it failed. I thought it was an issue that the controller did not know the 'informed' factory.

html

  & lt; Body ng-app = "myApp" & gt; Can anyone help? & Lt; Div id = "simple" ng-controller = "myController" & gt; & Lt; P & gt; Let's try this simple notification service, injector in the controller ... & lt; / P & gt; & Lt; Input ng-init = "message = 'test'" ng-model = "message" & gt; & Lt; Button ng-click = "call note (message);" & Gt; Notified & lt; / Button & gt; & Lt; P & gt; (You have to click 3 times to see a warning) & lt; / P & gt; & Lt; / Div & gt;   

JS

  & lt; Script & gt; // 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, Notification Service) {scope.callNotify = function (msg) {notifyService (msg); }; } MyController $ Injection = ['$ scope', 'notify']; // How to let the 'notify' factory in MyController MyServiceModule? // myApp angular.module ('myApp', []). Controller ('myController', myController); & Lt; / Script & gt;    

Your app must be declared dependent for other modules: < Pre> // myApp angular.module ('myApp', ["MyServiceModule"]). Controller ('myController', myController);

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -