2015-11-19 52 views
0

我发现了一些代码,我想复制/粘贴并在两个控制器中使用。它看着一些东西。

$scope.$watch('thing', function (thing) { 
    // do cool stuff with thing 
} 

而是复制/粘贴的,我希望把它放在一个服务,然后从两个控制器sortof这样使用的服务:

angular.module('myApp') 
.factory('CoolService', 
    function() { 
    $scope.$watch('thing', function (thing) { 
     // do cool stuff with thing 
    } 
} 

现在,如果我这样做,它赢得了” t知道什么$scope是吧? (根据一些阅读,它不会让我做,反正。

不过,我想说,如果你有这个服务,你得到这个手表

有一个提示我可以这样做:Passing current scope to an AngularJS Service

所以我把他的例子,固定它,并在那里工作scope.watch,但现在我无法设置表内的其他范围内的变量。我只是不知道足够的JavaScript来做到这一点,但我很接近。我真的认为这将与正确的语法工作...

angular.module('blah', []); 
 

 
angular.module('blah').factory('BlahService', function() { 
 
    //constructor 
 
    function BlahService(scope) { 
 
     this._scope = scope; 
 
     this.myFunc = function(){ 
 
     this._scope.otherVar = this._scope.someVar; 
 
     }; 
 
     this._scope.$watch('someVar', function(someVar) { 
 
     // do cool stuff with thing 
 
     _scope.otherVar = this._scope.someVar; // undefined 
 
     this._scope.otherVar = this._scope.someVar; // undefined 
 
     this.myFunc(); // undefined 
 
     BlahService.prototype._someFunction(); // works, but... 
 
     return someVar; 
 
     }); 
 

 
    } 
 

 
    //wherever you'd reference the scope 
 
    BlahService.prototype._someFunction = function() { 
 
    if (this._scope['someVar'] == 1) // undefined 
 
     this._scope['someVar']++; 
 
    } 
 

 
    return BlahService; 
 

 
}); 
 

 
angular.module('blah').controller('BlahCtrl', function($scope, BlahService) { 
 
    $scope.someVar = 4; 
 
    $scope.BlahService = new BlahService($scope); 
 
}); 
 

 
angular.module('blah').controller('Blah2Ctrl', function($scope, BlahService) { 
 
    $scope.someVar = 6; 
 
    $scope.BlahService = new BlahService($scope); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<html ng-app="blah"> 
 
    <body> 
 
    <div ng-controller="BlahCtrl"> 
 
     1a. <input ng-model="someVar"> 
 
     1b. <input ng-model="otherVar"> 
 
    </div> 
 
<div ng-controller="Blah2Ctrl"> 
 
     2. <input ng-model="someVar"> 
 
    2b. <input ng-model="otherVar"> 
 
    </div> 
 
    </body> 
 
</html>

的主要特征,这片段中的是,范围有不同的范围。它不像一个单身人士。

回答

0

这做了,它让我从表内设定的范围内的其他成员:

angular.module('blah', []); 
 

 
angular.module('blah').factory('BlahService', function() { 
 
    //constructor 
 
    function BlahService(scope) { 
 
    this._scope = scope; 
 
    this.myFunc = function() { 
 
     this._scope.otherVar = this._scope.someVar; 
 
    }; 
 
    this._scope.$watch('someVar', function(newValue, oldValue, scope) { 
 
     // do cool stuff with thing 
 
     scope.otherVar = Number(scope.someVar) + 1; 
 
     return newValue; 
 
    }); 
 
    } 
 
    return BlahService; 
 
}); 
 

 
angular.module('blah').controller('BlahCtrl', function($scope, BlahService) { 
 
    $scope.someVar = 4; 
 
    $scope.BlahService = new BlahService($scope); 
 
}); 
 

 
angular.module('blah').controller('Blah2Ctrl', function($scope, BlahService) { 
 
    $scope.someVar = 6; 
 
    $scope.BlahService = new BlahService($scope); 
 
});
<html ng-app="blah"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
    <body> 
 
    <div ng-controller="BlahCtrl"> 
 
     1a. <input ng-model="someVar"> 
 
     1b. <input ng-model="otherVar"> 
 
    </div> 
 
<div ng-controller="Blah2Ctrl"> 
 
     2. <input ng-model="someVar"> 
 
    2b. <input ng-model="otherVar"> 
 
    </div> 
 
    </body> 
 
</html>

0

将$ scopes传递给服务听起来像是内存泄漏的秘诀。如果没有别的,这是漫长的。

而是只考虑这样做在每个指令:

scope.$watch('thing', function (thing) { 
    coolService.doCoolStuffWith(thing); 
} 

让指令做了自己范围的观看,并把共享的功能的服务。

+0

我回答我自己的问题,但如果我的回答使我悲伤我会尝试你的。谢谢你的回答。也许我太干了...我们会看到。 – toddmo