[英]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.