我有以下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'}
];
每次从select form标签元素中选择一些内容时,我都会收到以下错误。
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”}
谢谢。
答案 0 :(得分:0)
这是我犯的错误。与我在问题中发布的相反,$ scope.bill被初始化为
$ scope.bill = null;
所以它抛出了错误。当我正确初始化它时,错误就消失了 $ scope.bill = [];
感谢@jaux帮助我找到问题