Undefined error for some variables in angular?

I use the following code in codefen and encounter this problem, for conctact I get the following error

Why does he give an error for the contact, and not for the 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 the 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 does it not work on contact, and not by name?
  • Contacts are numerical data, what should I give it by default?
+7
javascript html angularjs
source share
3 answers
  $scope.name = name; $scope.contact = contact; 

The error of sending it to a contact is that your application does not have a global contact variable, but if you go to the console and enter the name .. there is a global variable name equal to "" so that it does not give an error.

If you replace $scope.name any other variable, this will result in an error. Its all because the name is global, which equals an empty string.

A scenario where it throws age instead of contact . http://fiddle.jshell.net/o6a54Lw5/1/

A scenario where it throws contact instead of name . http://fiddle.jshell.net/o6a54Lw5/2/

Now in the second fiddle, if you go to the console and enter name , you will see its declared global .

+4
source share

Do not specify the name as name for the global variable, as it stands for window.name , because

window.name gets / sets the window name.

For this reason its never undefined and therefore $scope will accept it

+2
source share

Here is the problem

  $scope.contact = contact; 

You did not provide a contact and name anywhere.

Working APP

0
source share

All Articles