0

如果没有 ngcontroller 别名,我可以获取数据。但是,当使用别名时,我不能。我的错误是什么?

HTML 标签:

<div style="padding: 10px" id="content_dv" ng-controller="displaytopic_ctrl as f">
     <div class="topic_dv" ng-repeat="t in f.topic">
        <p>{{ t.MEMBER_NAME }}</p>
     </div>
</div>

在 app.js 中:

.controller('displaytopic_ctrl', ['$http', function($http) {

    $http({
        method: 'get',
        url: api_url + 'get_topic_list.php', 
        data: {
            type: 'all'
        }

    }).success(function(d){     
        if(d.t=='p'){
            this.topic = d.topic;
        }
    }).error(
    function(){
        console.log('Query error');
    });     

}]);
4

1 回答 1

0

由于JavaScript 闭包的工作this方式,您在成功回调中使用的变量不是控制器。解决此问题的最常用机制是为控制器创建一个别名,您可以在回调中引用该别名。

例如:

.controller('displaytopic_ctrl', ['$http',
  function($http) {
    var controller = this;

    $http({
      method: 'get',
      url: api_url + 'get_topic_list.php',
      data: {
        type: 'all'
      }

    }).success(function(d) {
      if (d.t == 'p') {
        controller.topic = d.topic;
      }
    }).error(
      function() {
        console.log('Query error');
      });

  }
]);
于 2015-10-23T07:31:02.737 回答