[英]AngularJS working with ng-change in a directive with isolated scope - Moving to component based architecture
我目前正在努力在我的指令中使用隔離范圍進行更改。 我試圖從ng-controller轉移到更加基於組件的架構,但它證明比預期更具挑戰性。
這是一個小提琴,我似乎無法工作。 小提琴
我很確定這個問題就在這里
app.directive("search", function(service) {
return {
restrict: 'E',
replace: true,
scope: {},
controller: ['$scope', function($scope) {
$scope.search = function(keyword) {
service.searchData(keyword);
};
}],
template: '<div style="padding-bottom: 15px;">' +
'<center>' +
'<input type="text" ng-model="keyword" ng-change="search(keyword)"/>' +
'</center>' +
'</div>'
};
});
但是小提琴將更全面地了解我想要完成的任務。
Chnage控制器如下圖所示
controller: searchCtrl
在指令之外寫下面的代碼
searchCtrl.$inject = ['$scope', 'service'];
function searchCtrl($scope, service) {
$scope.search = function(keyword) {
alert("called - " + keyword);
service.searchData(keyword);
};
}
Charan Cherry的答案是正確的,如果只使用角度,但我使用角度和requireJS一起,所以這是我做的解決問題。
由於ng-change沒有觸發,我只使用了ng-model,然后讓觀察者觀察變化,然后激活我的搜索功能。
希望這有助於某人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.