angularjs - can controllers create arbitrary properties in the $rootScope in Angular -


is legal controller in angular?

       $rootscope.somearbitaryobject = ["fee", "fie", "fo", "fum]; 

or

      $rootscope.foo = {name: "jane q. public", favoritecolor: "green"} 

yes, legal if want controllers have access model.

a better practice use services can inject 1 or more controllers:

var myapp = angular.module('myapp', []);  myapp.factory('myservice', function () {   return { message: "i'm data service" }; });  myapp.controller('firstctrl', function($scope, myservice) {   $scope.data = myservice; });  myapp.controller('secondctrl', function($scope, myservice) {   $scope.data = myservice; }); 

any change make myservice properties in 1 controller affect other controllers use myservice.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -