![](/img/trans.png)
[英]Can't access template created through innerHTML directive, Angular 4
[英]angular can't access directive property
我有以下帶有指令的應用程序:
(function() {
var app = angular.module('myFeed', []);
app.directive("feedList", function(){
console.log('in feed list directive');
return {
restrict: 'E',
templateUrl: "/static/angular/phone-list/feed-list.template.html",
cotrollerAs: 'myController',
controller: function($http){
var data = $.param({
grant_type: 'password',
username: 'test',
password: 'test',
client_id:'test',
client_secret:'test',
});
var config = {
headers : {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
}
$http.post('/o/token/', data, config)
.success(function (data, status, headers, config) {
access_token = data['access_token'];
console.log(access_token);
$http({method: 'GET', url: 'api/posts/', headers: {'Authorization': 'Bearer '+access_token}}).then(function(response) {
this.posts = response.data;
});
})
.error(function (data, status, header, config) {
$scope.ResponseDetails = "Data: " + data +
"<hr />status: " + status +
"<hr />headers: " + header +
"<hr />config: " + config;
});
var header = {
headers : {
'Authorization': 'Bearer '+self.access_token
}
}
},
}
});
})();
我在index.html中的指令如下所示:
<feed-list></feed-list>
模板如下所示:
<li ng-repeat="post in myController.posts>{{post}}</li>
如何獲取<feed-list>
以顯示在this.posts
設置的this.posts
?
JavaScript函數的更改范圍。 對此進行參考
var myController = this;
在控制器的構造函數的開頭,然后使用
myController.posts = response.data;
而不是
this.posts = response.data;
在您的http回調函數中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.