尝试将一个变量复制到另一个变量时遇到错误
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$scope.myWelcome = "Welcome"
var vm = this,
parent = $scope.summary,
results = $scope.results,
matches = {
"value":"John"
};
parent.def = angular.copy(matches);
$scope.myWelcome = parent.def;
});
//面对此错误,请有人指导:
TypeError: Cannot set property 'def' of undefined
at Object.<anonymous> (<anonymous>:11:14)
at Object.invoke (angular.js:5106)
at O.instance (angular.js:11076)
at p (angular.js:9939)
at f (angular.js:9248)
at angular.js:9113
at angular.js:1960
at m.$eval (angular.js:18542)
答案 0 :(得分:0)
在这一行:parent.def = angular.copy(matches);
,parent
等于$scope.summary
。
因此,您尝试做等效于$scope.summary.def = angular.copy(matches)
的事情。由于$scope.summary
未定义,因此会引发错误。
您可以通过首先将$scope.summary
定义为一个空对象$scope.summary = {}
来解决此问题。