最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught ReferenceError: $scope is not defined - Stack Overflow

programmeradmin3浏览0评论

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

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 = {}
};

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

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 = {}
};
Share Improve this question edited Aug 19, 2015 at 9:41 Marek Lipka 51.2k7 gold badges89 silver badges92 bronze badges asked Aug 19, 2015 at 9:29 Billy LoganBilly Logan 2,5206 gold badges29 silver badges48 bronze badges 1
  • Did you add the references for the jscript before your own scripts? <script language="JavaScript" type="text/javascript" src="/js/jquery-1.2.6.min.js"></script> <script language="JavaScript" type="text/javascript" src="/js/myscripts"></script> – SWiggels Commented Aug 19, 2015 at 9:32
Add a ment  | 

2 Answers 2

Reset to default 9

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 = {}
   };
});

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 = {}
};
发布评论

评论列表(0)

  1. 暂无评论