search

Home  >  Q&A  >  body text

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

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

仅有的幸福仅有的幸福2744 days ago530

reply all(2)I'll reply

  • 过去多啦不再A梦

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

    It means you haven’t defined itsearchObj. At least you can define this object:

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

    reply
    0
  • PHP中文网

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

    It’s because your $scope.searchObj has no value assigned, and the result becomes undefined.name, which must be an error

    reply
    0
  • Cancelreply