Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught ReferenceError: $scope is not defined

Receiving the error when the page is loaded. I'm trying to append a new object to an array of entries.

enter image description here

enter image description here

What is wrong with the code?

index.html

Raffler

<div ng-controller="RaffleCtrl">

  <form ng-sumbit="addEntry">
    <input type="text" ng-model="newEntry.name">
    <input type="submit" value="Add">
  </form>

  <ul>
    <li ng-repeat="entry in entries">{{entry.name}}</li>
  </ul>
</div>

raffle.js

angular.module('myApp', []).controller("RaffleCtrl", function ($scope) {
  $scope.entries = [
      {
        name: "Larry"
      }, {
        name: "Curly"
      }, {
        name: "Moe"
      }
    ]
});

$scope.addEntry = function () {
  $scope.entries($scope.newEntry)
  $scope.newEntry = {}
};
like image 476
Billy Logan Avatar asked Aug 19 '15 09:08

Billy Logan


People also ask

How do I fix uncaught ReferenceError is not defined?

The $ is not defined ReferenceError usually arises when jQuery is not loaded and JavaScript is not recognizing the $ symbol. To solve this error, first, use jQuery CDN link inside the head section or download the jQuery file and use the jQuery file link inside the head section.

How do I fix JavaScript reference error?

Reference errors in Javascript are mainly thrown when an attempt is made to reference a variable that does not exist or is out of scope. Therefore, in the majority of cases, a ReferenceError can be fixed by making sure that the referenced variable is defined correctly and is being called in the correct scope.

What is uncaught ReferenceError in JavaScript?

The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading.

What is uncaught ReferenceError?

Ad. While programming in JavaScript, jQuery or Angular JS, coders often encounter an error called Uncaught ReferenceError: $ is not defined. This usually happens when the $, that may be a variable or a method is not properly defined but is being used.


2 Answers

You wrongly used $scope outside the controller. Use the $scope inside the controller

angular.module('myApp', []).controller("RaffleCtrl", function ($scope) {
  $scope.entries = [
      {
        name: "Larry"
      }, {
        name: "Curly"
      }, {
        name: "Moe"
      }
    ];

   $scope.addEntry = function () {
     $scope.entries($scope.newEntry)
     $scope.newEntry = {}
   };
});
like image 187
Sudharsan S Avatar answered Sep 22 '22 05:09

Sudharsan S


if you really want to keep it outside

angular.module('myApp', []).controller("RaffleCtrl", function ($scope) {
  $scope.entries = [
      {
        name: "Larry"
      }, {
        name: "Curly"
      }, {
        name: "Moe"
      }
    ];
  $scope.addEntry = addEntry;
});

function addEntry() {
  $scope.entries($scope.newEntry)
  $scope.newEntry = {}
};
like image 34
David Fregoli Avatar answered Sep 26 '22 05:09

David Fregoli