2016-07-26 87 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

相關問題