【发布时间】:2016-01-05 10:05:42
【问题描述】:
我需要一个可以为我提供范围或动态 var 的服务,所以我转向其他控制器。
我对 JSBin 进行了测试,但无法正常工作。
https://jsbin.com/semozuceka/edit?html,js,console,output
angular.module('app', [])
.controller('control1', function($scope, shared) {
shared.set('teste', {
testecontroller1: "Apenas um teste"
});
$scope.teste = shared.get();
$scope.teste2 = shared.get();
})
.controller('control2', function($scope, shared) {
$scope.teste = shared.get('teste');
shared.set('teste2', {
testecontroller2: "Apenas um teste"
});
$scope.teste2 = shared.get('teste2');
})
.service('shared', function($scope) {
$scope.data = {};
this.set = function(key, obj) {
$scope.data[key] = obj;
};
this.get = function(key) {
return $scope.data[key];
};
});
【问题讨论】:
-
你不能在你的服务中注入 $scope。
标签: angularjs angular-services