2016-07-26 88 views
1

不更新我的组件的资料,因此,我的问题是这样的:角 - 我的控制器在视图

我的影片用了两次一个组成部分,它定义了一个模板。

组件:

.component('topList', { 
    template: '<div class="well"> \ 
       <div class="title"> \ 
       <h3>{{$ctrl.model.title}}</h3><a href="#" class="close"><i class="ss-delete"></i></a> \ 
       </div> \ 
       <div ng-repeat="i in $ctrl.model.items" class="list-question"> \ 
       <div class="item"><span class="number">{{$index + 1}}</span> \ 
       <div class="description"> \ 
       <h4>{{(i.Count/ $ctrl.model.total)*100 | number:2}}% ({{i.Count}} de {{ $ctrl.model.total }})</h4> \ 
       <p>{{i.Description}}</p> \ 
       </div> \ 
       </div> \ 
       </div> \ 
       </div>', 
    controller: TopListController, 
    bindings: { 
     model: '=' 
    } 
}) 

function TopListController() { 

} 

控制器:

.controller('myController', function ($http, $scope, $controller, datesService) { 
    var ctrl = this; 

    $scope.api = function() { 
      var date = datesService.getDates(); 
      $http.get("apicall/date.from/date.to").then(function (response) { 
       ctrl.model = { 
        title: 'Title', 
        items: response.data.Items, 
        total: response.data.Total 
       }; 
      }); 
    } 

    $http.get("apicall/date1/date2").then(function (response) { 
     ctrl.model = { 
      title: 'Title', 
      items: response.data.Items, 
      total: response.data.Total 
     }; 
    }); 
}) 

HTML(玉实际上):

div(ng-controller="myController as controller") 
     top-list(model="controller.model") 

当页面被加载时,该组件被填充来自所述数据方法$ http.get被称为控制器被创建,但是当我从html调用方法api()时,dat一个不改变。

我从另一个控制器调用方法api()。

+1

你混合'controllerAs'用' $ scope'控制器内部,所以你可能会感到困惑。你应该理想地放在控制器上下文中('this')。然后你可以调用方法,像'ng-click =“$ ctrl.api()”' –

+0

我没有明白,我忘记提到即时通讯从另一个控制器调用方法api。 – dpolicastro

+0

你能解释一下“从另一个控制器调用方法api”部分吗?你的意思是api方法没有从与控制器相对应的模板调用? –

回答

0
  1. 如果您使用您提取的数据更新视图,则不需要拨打$scope.$apply();。你已经有观察家设置使用2个数据绑定

  2. $http.get("apicall/date.from/date.to) 
    

    应该是这样的(你忘了"

    $http.get("apicall/date.from/date.to") 
    
+0

我忘记了“编辑时,它的真实我实际上不需要$ scope。$ apply(),但它不改变视图,我不知道它是如何工作的,即使经过很多研究。 – dpolicastro

+0

你使用'如果你使用$ scope的话,controllerAs'看起来就像你不这样做。在你的ctrl中尝试改变'var ctrl = this;'到'var ctrl = $ scope;' – svarog

+0

Im not using controllerAs ..但我想我会拥有做一个关于它的研究..我做了改变,它不工作.. – dpolicastro

相关问题