javascript - How to share the $scope variable of one controller with another in AngularJS? -
i have this:
app.controller('foo1', function ($scope) { $scope.bar = 'foo'; }); app.controller('foo2', function ($scope) { // want access $scope of foo1 here, access bar });
how accomplish this?
you use angular service share variable acrosss multiple controllers.
angular.module('myapp', []) .service('user', function () { return {}; })
to share data among independent controllers, services can used. create service data model needs shared. inject service in respective controllers.
function controllera($scope, user) { $scope.user = user; $scope.user.firstname = "vinoth"; } function controllerb($scope, user) { $scope.user = user; $scope.user.lastname = "babu"; }
Comments
Post a Comment