Luc*_*emy 31 javascript angularjs angularjs-scope angularjs-controller
我有这个:
app.controller('foo1', function ($scope) {
$scope.bar = 'foo';
});
app.controller('foo2', function ($scope) {
// want to access the $scope of foo1 here, to access bar
});
Run Code Online (Sandbox Code Playgroud)
我怎么做到这一点?
Tha*_*var 40
您可以使用Angular Service共享可变acrosss多个控制器.
angular.module('myApp', [])
.service('User', function () {
return {};
})
Run Code Online (Sandbox Code Playgroud)
要在独立控制器之间共享数据,可以使用服务.使用需要共享的数据模型创建服务.在相应的控制器中注入服务.
function ControllerA($scope, User) {
$scope.user = User;
$scope.user.firstname = "Vinoth";
}
function ControllerB($scope, User) {
$scope.user = User;
$scope.user.lastname = "Babu";
}
Run Code Online (Sandbox Code Playgroud)
app.controller('foo2', function ($scope) {
$scope.$$prevSibling.bar="bar"
});
Run Code Online (Sandbox Code Playgroud)