簡體   English   中英

未捕獲的 ReferenceError: $scope 未定義

[英]Uncaught ReferenceError: $scope is not defined

加載頁面時收到錯誤。 我正在嘗試將一個新對象附加到一個條目數組中。

在此處輸入圖片說明

在此處輸入圖片說明

代碼有什么問題?

索引.html

抽獎者

<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>

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

您錯誤地在控制器之外使用了$scope 在控制器內使用$scope

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

   $scope.addEntry = function () {
     $scope.entries($scope.newEntry)
     $scope.newEntry = {}
   };
});

如果你真的想把它放在外面

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM