Angularjs指令追加$http请求
Angularjs directive append $http request
我想创建一个在ajax调用后附加新指令的angularjs指令。
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 result之后附加指令。但它在前面。我应用$scope.$watch(scope. searchresult),但不工作
controller: function($scope){
$scope.search = function(){
$http({url: "..."})
.then(function(response){
$scope.searchResult = response.data;
});
}
},
link: function(scope,element){
$scope.$watch('searchResult', function(results) {
if (results) {
var directiveHtml = ('<search-result></search-result>');
var directiveElement = $compile(directiveHtml)(scope);
element.append(directiveElement);
}
})
}
或者在html
ng-if
controller: function($scope, $http){
$scope.search = function(){
$http({url: "..."})
.then(function(response){
$scope.searchResult = response.data;
var directiveHtml = ('<search-result></search-result>');
var directiveElement = $compile(directiveHtml)($scope);
angular.element(document.getElementById('id')).append(directiveElement);
});
}
}
相关文章:
- Meteor如何接收HTTP请求
- 在我的情况下,如何进行http请求
- 使用插件收听Firefox标签的http请求
- http请求使用html而不是json进行响应
- 在node-js路由中发出http请求
- 是否可以在没有HTTP请求或<输入>
- Acync JS HTTP请求通知请求
- PhantomJs在控制台中发出http请求时不会返回任何状态
- PHP处理来自一个页面的http请求,并将其显示在另一个页面上
- Firefox插件-如何发出http请求
- 验收测试主要针对传出的http请求
- 验证HTTP请求来自NFC标签上的点击
- 如何制作'http请求'在Node js中同步
- 为什么我在Cloud9中的http请求总是返回错误
- AngularJS Typeahead和$http请求限制
- HTTP请求使用cURL,但在浏览器中使用javascript时会得到CORS响应
- 如何使用Chrome扩展API获取网页的所有HTTP请求
- 将http请求中的数据传递给C#控制器
- 在初始化路由之前发送$http请求
- 从客户端捕获 HTTP 请求(包括 ajax)