gpt4 book ai didi

javascript - Angular 无法访问指令属性

转载 作者:行者123 更新时间:2023-12-03 06:07:45 25 4
gpt4 key购买 nike

我有以下带有指令的应用程序:

(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 中设置的帖子?

最佳答案

JavaScript 函数中此更改的范围。保留对此的引用

var myController = this;

在 Controller 构造函数的开头,然后使用

myController.posts = response.data;

而不是

this.posts = response.data;

在你的http回调函数中。

关于javascript - Angular 无法访问指令属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39459271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com