2015-11-19 68 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

相關問題