簡體   English   中英

在為ng-model設置之前執行ng-change <select>

[英]ng-change executing before ng-model is set for a <select>

我有以下html和js

 <select ng-change="setBillGroup()" ng-model="bill.groupId" class="span8" ng-options="d.id as d.name for d in groups"></select>

和相應的js是

   $scope.bill=[];
   $scope.setBillGroup = function() {
   var $groupId = $scope.bill.groupId; .... }

這是$ scope.groups JSON

 $scope.groups= [
{id:'1',users:[1,2,3,4],name:'SLC '},
{id:'2',users:[1,2],name:'Denver'},
{id:'3',users:[2,3],name:'Ski Club'},
{id:'4',users:[2,5],name:'Family'},
{id:'5',users:[1,2,3,5],name:'whatever'}
];

每當我從選擇表單標簽元素中選擇某些內容時,我都會不斷收到以下錯誤。

TypeError: Cannot read property 'groupId' of null
at Object.$scope.setBillGroup

即使設置了ng-model,似乎ng-change功能仍在運行。 有沒有一種方法可以讓ng-change函數setBillGroup()在用戶選擇項目后等待ng-model的設置? 我確實檢查了$ scope.bill的值,並且{{bill}}的值正確設置為{“ groupId”:“ 1”}

謝謝。

這是我犯的錯字。 與我在問題$ scope.bill中發布的相反,它已初始化為

$ scope.bill = null;

所以它拋出了錯誤。 當我正確地初始化它時,錯誤消失了$ scope.bill = [];

感謝@jaux幫助我發現問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM