Javascript 在为a设置ng模型之前执行ng更改<;选择>;

Javascript 在为a设置ng模型之前执行ng更改<;选择>;,javascript,select,angularjs,angularjs-directive,angularjs-scope,Javascript,Select,Angularjs,Angularjs Directive,Angularjs Scope,我有以下html和js <select ng-change="setBillGroup()" ng-model="bill.groupId" class="span8" ng-options="d.id as d.name for d in groups"></select> 下面是$scope.groups JSON $scope.groups= [ {id:'1',users:[1,2,3,4],name:'SLC '}, {id:'2',users:[1,2]

我有以下html和js

 <select ng-change="setBillGroup()" ng-model="bill.groupId" class="span8" ng-options="d.id as d.name for d in groups"></select>
下面是$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'}
];
每次我从selectform标记元素中选择某些内容时,都会出现以下错误

TypeError: Cannot read property 'groupId' of null
at Object.$scope.setBillGroup
即使在设置ng模型之前,ng更改功能仍在运行。有没有办法让ng更改函数setBillGroup()在用户选择项目后等待ng模型的设置?我确实检查了$scope.bill的值,{{bill}}值被正确设置为{“groupId”:“1”}


谢谢。

这是我的打字错误。与我在问题中发布的相反,$scope.bill被初始化为

$scope.bill=null

所以它抛出了错误。当我正确初始化它时,错误消失了 $scope.bill=[]


@jaux帮我找到了这个问题,真是太好了

即使在控制器顶部初始化
$scope.bill
,它也会给你一个错误吗?@Toshishimayama它被设置为[]($scope.bills=[];),所以我希望在setBillGroup()函数中访问它之前使用ng模型来设置这个值。另外,我编辑了这个问题来发布这个信息。谢谢。我在运行你的代码时没有收到任何错误。看一看@jaux谢谢!这是我做的蠢事。似乎将$scope.bill初始化为null,而不是我在原始问题中所问的[]。我只是再次检查了代码,发现了打字错误。我很高兴程序员能够以这种方式互相帮助,并以积极的方式成长。非常感谢。
TypeError: Cannot read property 'groupId' of null
at Object.$scope.setBillGroup