search

Home  >  Q&A  >  body text

angular.js - An error occurred when adding a new attribute assignment in angularjs. What happened?

For example, I pass in a serach object below

In html web page:

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

In js.....

$scope.search = function(searchObj){

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

}

console.log($scope.Temp.corporationId);
There is value here.
But $scope.searchObj.name=$scope.Temp.corporationId;
An error occurs when assigning like this, saying name is not defined. What's going on?
angular.yh.js:12454 TypeError: Cannot set property 'name' of undefined

To solve, how to assign value? ? How to assign the value on the right to the new attribute of the coordinate?

仅有的幸福仅有的幸福2784 days ago565

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