简体   繁体   English

角度无法访问指令属性

[英]angular can't access directive property

I have the following app with directive: 我有以下带有指令的应用程序:

(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
                      }
                  }



          },

      }
  });



})();

My directive in index.html looks like this: 我在index.html中的指令如下所示:

<feed-list></feed-list>

The template looks like this: 模板如下所示:

<li ng-repeat="post in myController.posts>{{post}}</li>

How can I get the <feed-list> to display the posts set in this.posts ? 如何获取<feed-list>以显示在this.posts设置的this.posts

The scope of this changes in JavaScript functions. JavaScript函数的更改范围。 Keep a reference to this with a 对此进行参考

var myController = this;

at the beginning of your controller's constructor function and then use 在控制器的构造函数的开头,然后使用

myController.posts = response.data;

rather than 而不是

this.posts = response.data;

in your http callback function. 在您的http回调函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM