Heim  >  Fragen und Antworten  >  Hauptteil

angular.js - 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

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

仅有的幸福仅有的幸福2713 Tage vor513

Antworte allen(2)Ich werde antworten

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-15 17:04:14

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

    $scope.searchObj = {};
    
    $scope.search = function(searchObj){
        console.log($scope.Temp.corporationId);
        $scope.searchObj.name=$scope.Temp.corporationId;
    };

    Antwort
    0
  • PHP中文网

    PHP中文网2017-05-15 17:04:14

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

    Antwort
    0
  • StornierenAntwort