首页 > angularjs添加新属性赋值,发生错误,怎么回事呢?

angularjs添加新属性赋值,发生错误,怎么回事呢?

如,我下面传入一个serach对象

在html网页中:

<select ng-model="Temp" ng-options="c.abbrZh for c in corpss">

在js中.....

$scope.search = function(searchObj){

console.log($scope.Temp.corporationId);
$scope.searchObj.name=$scope.Temp.corporationId;

}

console.log($scope.Temp.corporationId);
这里是有值的。
但是$scope.searchObj.name=$scope.Temp.corporationId;
这样赋值就发生错误,说name没有定义,怎么回事呢?
angular.yh.js:12454 TypeError: Cannot set property 'name' of undefined

求解,如何赋值??如何把右边的赋值给坐标的新的属性呢?


是你的$scope.searchObj没有赋值,结果变成undefined.name,肯定报错啊


说明你没定义searchObj,好歹你也把这个对象定义一下呗:

$scope.searchObj = {};

$scope.search = function(searchObj){
    console.log($scope.Temp.corporationId);
    $scope.searchObj.name=$scope.Temp.corporationId;
};
【热门文章】
【热门文章】