admin管理员组文章数量:1320661
I am using the following code in codepen and facing this issue, for conctact i am getting the following error
Why is it giving error for contact and not for name?
How can i solve this ?
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)
Here is js file
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);
}]);
Here is the HTML file that i am using.
<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>
PLEASE answer these questions
- Why is it failing at contact and not at name ?
- Contact is number data, what should i give the default value to it ?
I am using the following code in codepen and facing this issue, for conctact i am getting the following error
Why is it giving error for contact and not for name?
How can i solve this ?
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)
Here is js file
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);
}]);
Here is the HTML file that i am using.
<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>
PLEASE answer these questions
- Why is it failing at contact and not at name ?
- Contact is number data, what should i give the default value to it ?
-
1
what you are getting in contact that you are assigning to
$scope.contact
? – Hardik Vaghani Commented Aug 3, 2016 at 11:10
3 Answers
Reset to default 4 $scope.name = name;
$scope.contact = contact;
Its throwing error for contact is because there is no global contact variable in your app, but if you go to console and type name.. there is a global variable name which equals ""
so it does not throw error.
If you replace $scope.name
with any other variable, it would throw error for that. Its all because name is global which equals empty string.
A fiddle where it throws for age
instead of contact
.
http://fiddle.jshell/o6a54Lw5/1/
A fiddle where it throws for contact
instead of name
.
http://fiddle.jshell/o6a54Lw5/2/
Now in the second fiddle if you go to console and type name
, you would see its declared global
.
Do not give name as name
for a global variable as it denotes window.name
because,
window.name gets/sets the name of the window.
For that reason, its never undefined and so the $scope
will accept it
Here is the issue,
$scope.contact = contact;
you have not defined contact and name anywhere.
Working APP
本文标签: javascriptNot defined error for some variables in angularStack Overflow
版权声明:本文标题:javascript - Not defined error for some variables in angular? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742063916a2418716.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论