不能在控制器中使用函数

时间:2017-09-11 13:56:20

标签: javascript angularjs

函数GameController($ scope)不起作用,我不知道为什么,有人可以帮忙吗?

 (function () {
  angular.module('MemoryGame').controller('GameController', GameController);

  GameController.$inject = ['$scope'];
  console.log("hhhhhhh");


  function GameController($scope) {
    console.log("gggggg");

    var cards = document.querySelectorAll(".all-cards");
    var i = 0;
    var textArray = []; // array that saving the text of the car
    var count = 0; // count the card flipped
    var save_cards = [];



    cards.forEach((card) => {
      card.addEventListener('click', function () {


      });
    });
  }
})();

1 个答案:

答案 0 :(得分:0)

不确定您收到了什么错误 - 但尝试设置控制器的方式略有不同。您可以在声明控制器的同时注入$ scope对象。

var app = angular.module('MemoryGame', []);

app.controller('GameController', function($scope) {

    var cards = document.querySelectorAll(".all-cards");
    var i = 0;
    var textArray = []; // array that saving the text of the car
    var count = 0; // count the card flipped
    var save_cards = [];

    cards.forEach((card) => {
      card.addEventListener('click', function () {
    });
});

另外,您是否在HTML中正确声明了ng-app和ng-controller?

<body ng-app="MemoryGame">
    <div ng-controller="GameController">
        {{hello}}
    </div>
</body>