I have scope
value which I need in rootScope
so I can use it across app, and so I did it like
var app = angular.module('myApp', []);
app.run(function($rootScope) {
$rootScope.car= 'honda';
});
app.controller('myCtrl', function($scope) {
$scope.car= "suzuki";
});
I am accessing {{car}}
in my index file and its giving me honda, that is fine.
What I need is same value of $scope.car
in $rootScope.car
to access it global as I wont be able to access $scope.car value.
The reason I want same value is in $scope.car
, I will have my service value which is dynamically coming from json parse after long code and to get in rootscope
, to write service again is not a good practice.
Any other way, that In same controller function , I can have $rootScope
just like $scope
which I can use global ?
Or any other options ?
Thanks.
In your controller, you can use $rootScope, as every application has a single root scope. You don't have to have $scope.car in your controller, you can use $rootscope.car in your controller too.
You can broadcast a event from your child controller
$rootScope.$emit('eventname',obj)
and in the parent controller you can use
$rootScope.$on('eventname',function(event,data){
console.log(data)
})
this way you can access a variable in child controller to the topmost rootscope where eventname is any event you want to broadcast
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.