2015-07-20 93 views
0

ng-change不會觸發我的函數,這裏是視圖;ng-change不會觸發

  <div ng-controller="widgets.lunchMenu as vm"> 
        <label class="btn btn-transparent grey-salsa btn-circle btn-sm active"> 
         <input type="radio" name="options" class="toggle" id="option1" ng-model="vm.takeCount" ng-value="0" ng-change="vm.getLunchMenus()">@L("Today") 
        </label> 
        <label class="btn btn-transparent grey-salsa btn-circle btn-sm"> 
         <input type="radio" name="options" class="toggle" id="option2" ng-model="vm.takeCount" ng-value="7" ng-change="vm.getLunchMenus()">@L("Week") 
        </label> 
        <label class="btn btn-transparent grey-salsa btn-circle btn-sm"> 
         <input type="radio" name="options" class="toggle" id="option3" ng-model="vm.takeCount" ng-value="30" ng-change="vm.getLunchMenus()">@L("Month") 
        </label> 
      </div> 

這裏是控制器:

(function() { 
    appModule.controller('widgets.lunchMenu', [ 
    '$scope', 'abp.services.app.lunch', 
    function ($scope, appService) { 
     var vm = this; 
     var today = new Date(); 
     var month = today.getMonth(); 

     vm.getLunchMenus = function() { 
      appService.getLunchMenus({ month: month + 1, takeCount: vm.takeCount }).success(function (data) { 
       vm.menus = data.items; 
      });; 
     }; 

     vm.getLunchMenus(); 
    } 
]); 
})(); 

什麼建議嗎?感謝您的幫助。

+0

你使用什麼版本的角? –

+1

建議:定義'vm = $ scope'或將綁定改爲'$ scope.getLunchMenus()'。你的HTML只與'$ scope'變量和函數交互。 – ryanyuyu

+1

@ryanyuyu海報似乎使用ControllerAs語法(儘管使用'vm'作爲每個控制器的通用名稱有點問題)。改變'vm'指向'$ scope'沒有任何意義。 – Claies

回答

1

爲使ng-change指令能夠看到vm.getLunchMenus函數,它必須位於$scope上。所以,你需要做的線沿線的東西:

$scope.vm = this; 
$scope.vm = function() { ... } 

然後在你的標記,你可以做你與

ng-change="vm.getLunchMenus()" 

做什麼,或者你可以做一些事情那樣簡單

$scope.getLunchMenus = function() { ... } 

然後在標記:

ng-change="getLunchmenus()" 

不要完全刪除了vm變量的需要,因爲this並不真正意味着什麼在標記的指令(ng-change等)。

+1

海報並未顯示定義控制器的完整HTML代碼,但它們似乎使用ControllerAs語法,這是構建數據的有效方法;你在這裏暗示他們放棄了這個語法,而是回過頭去使用'$ scope',這不太可能解決他們的核心問題,即函數沒有被觸發。 – Claies

+0

Claies是對的。我正在使用涵蓋虛擬機的ControllerAs語法。問題是別的。 – user3055125

+0

對不起,我不熟悉ControllerAs語法。 – Cineris