我想創建一個angularjs指令,在ajax調用後追加新指令。
var app = angular.module("app",[]);
// This directive is simple search operation.
app.directive("search", function("$http", function($http){
return {
restrict: "E",
template: '<input type="text" ng-model="searchText"/> <button ng-click="search()">Search</button>',
controller: function($scope){
$scope.search = function(){
$http({url: "..."})
.then(function(response){
$scope.searchResult = response.data;
});
}
},
link: function(scope,element){
var directiveHtml = ('<search-result></search-result>');
var directiveElement = $compile(directiveHtml)(scope);
element.append(directiveElement);
}
}
});
app.directive("searchResult", function(){
return {
restrict: "E",
template: "list of the search result",
link: function(scope,element){
}
}
});
我想在$ http結果後追加指令。但它之前附加。 我應用了$ scope。$ watch(scope.searchResult)但不起作用。
$手錶也應該有一個處理程序。即當值改變時執行的函數 - '$ scope。$ watch([返回監視值的表達式],[change handler],[objectEquality?]);' –