angular can访问指令属性

时间:2016-09-12 21:46:47

标签: javascript angularjs

我有以下app指令:

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

1 个答案:

答案 0 :(得分:1)

JavaScript函数中此更改的范围。用

来引用它
var myController = this;

在控制器的构造函数的开头,然后使用

myController.posts = response.data;

而不是

this.posts = response.data;

在您的http回调函数中。

相关问题