我在codepen中使用以下代码并面临此问题,
为了conctact我收到以下错误
为了conctact我收到以下错误
为什么它会给出错误的联系而不是名字?
我怎么解决这个问题?
angular.js:13550 ReferenceError: contact is not defined at new <anonymous> (pen.js:8) at Object.invoke (angular.js:4665) at R.instance (angular.js:10115) at n (angular.js:9033) at g (angular.js:8397) at g (angular.js:8400) at angular.js:8277 at angular.js:1751 at n.$eval (angular.js:17229) at n.$apply (angular.js:17329)
这是js文件
var app = angular.module("crud",[]); app.controller("ctrl",['$scope',function($scope) { $scope.data = [3,4,5,34,34]; debugger; $scope.name = name; $scope.contact = contact; $scope.obj = { name: $scope.name,contact: $scope.contact }; console.log($scope.obj); }]);
这是我正在使用的HTML文件.
<body ng-app="crud"> <div ng-controller="ctrl"> <div> <table> <tr ng-repeat="x in data track by $index"> <td>{{x}}</td> <td>{{$index}}</td> </tr> </table> </div> </div> </body>
请回答这些问题
>为什么联系失败而不是名字?
>联系人是号码数据,我应该给它什么默认值?
解决方法
$scope.name = name; $scope.contact = contact;
它的联系抛出错误是因为您的应用程序中没有全局联系变量,但如果您转到控制台并输入名称..有一个全局变量名称等于“”,因此它不会抛出错误.
如果将$scope.name替换为任何其他变量,则会为此抛出错误.它的全部是因为name是全局的,等于空字符串.
它是一个小提琴,它是为了年龄而不是接触而抛出的.
http://fiddle.jshell.net/o6a54Lw5/1/
它是一个小提琴,用于联系而不是名字.
http://fiddle.jshell.net/o6a54Lw5/2/
现在,如果你去控制台并输入名称,你会看到它声明的全局.