我熟悉AngularJS中的controllerAs語法,當我需要對服務變量進行簡單綁定時,我遇到了一個問題。通常$scope.$watch
或$scope.$on
會這樣做,但是這將涉及注入$scope
,這似乎違背了控制器的目的。
目前我有什麼是點擊一個按鈕,並呼籲config.setAttribute(attr)
後,控制器調用服務的setAttribute
功能,但不getAttribute
,所以config.attribute
永遠不會改變。
有什麼我忽略了我如何接近這個?我需要注入$scope
還是改變控制器的語法來改爲使用$scope
?
查看:
<div data-ng-controller="ConfigCtrl as config">
<h3>Customize</h3>
<pre>Current attribute: {{config.attribute}}</pre>
<label>Attributes</label>
<div data-ng-repeat="attr in config.attributes">
<button ng-click="config.setAttribute(attr)">{{attr.name}}</button>
</div>
</div>
服務:
(function() {
'use strict';
angular.module('app')
.factory('Customization', Customization);
function Customization() {
var service = {
attribute: null,
getAttributes: getAttributes,
setAttribute: setAttribute,
getAttribute: getAttribute
}
return service;
/////
function getAttributes() {
return [
{name: 'Attr1', value: '1'},
{name: 'Attr2', value: '2'} // etc.
];
}
function setAttribute(attr) {
service.attribute = attr;
}
function getAttribute() {
return service.attribute;
}
}})();
控制器:
(function(){
'use strict';
angular.module('app')
.controller('ConfigCtrl', ConfigCtrl);
function ConfigCtrl(Customization){
var vm = this;
vm.attribute = Customization.getAttribute(); // bind
vm.attributes = [];
// Functions
vm.setAttribute = Customization.setAttribute;
init();
/////
function init(){
// Get attributes array
vm.attributes = Customization.getAttributes();
}
}})();
注入'$ scope'並不違背控制器的目的。這很重要 - 當需要將'$ scope'作爲'$ watch'和'$ on'之類的服務注入時,而不是默認爲了發佈ViewModel屬性。 –
http://www.fullstacktraining.com/articles/angularjs-using-watch-with-controlleras –
感謝您的評論,他們絕對清除了我的困惑。 – bliptych