2014-11-09 2 views
-1

Мне нужно добавить пользовательскую директиву в мой код, но каждый раз, когда я его добавляю, он разбивает мой код. Я проверил пульт и дает мне следующую ошибкуПользовательский код нарушения директивы в AngularJS

Error: Argument 'guessGameController' is not a function, got undefined 
at Error (native) 

Теперь я не уверен, если я не устанавливать мой код права или если я не добавить директиву, где, как предполагается идти. Вот мой код, я ценю всю помощь.

index.html

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="guessGameApp"> 
<head> 
    <title>Word Game 2.0 - AngularJS</title> 
    <!--Encoding--> 
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type"> 
    <meta content="utf-8" http-equiv="encoding"> 

    <!-- JQuery --> 
    <script src="js/jquery-1.11.0.min.js"></script> 

    <!--Scripts--> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js" type="text/javascript"></script> 
    <script src="js/controllers/app.js" type="text/javascript"></script> 
    <script src="js/controllers/maincontroller.js" type="text/javascript"></script> 

    <!--Styles--> 
    <link rel="stylesheet" type="text/css" href="css/magicWord.css"> 
    <!--<script src="js/jquery-1.11.0.min.js"></script>--> 
</head> 
<body> 
<div ng-controller="guessGameController"> 
    <p> 
     <header id="masthead"> 
      <h2 align="center">{{appTitle}}</h2> 
     </header> 
    </p> 
    <div ng-controller="wordController"> 
     <p> 
      <table align="center" width="300px" height="150px" border="solid 2px"> 
       <tr> 
        <td id="guessBox"> 
         <p align="center"> 
          <input value="" type="text" id="guestGuess" placeholder="Enter Guess" ng-model="guestGuess"/> 
         </p> 
         <p align="center"><button ng-click="addGuess()" id="guessButton">Click to Check</button></p> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <h3 align="center">Your guesses so far are: </h3> 
         <p align="center" ng-repeat="words in guesses">{{words}}</p> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <p align="center">You have guessed:<b>{{guessed}}</b> times out {{allowed}} chances.</p> 
         <p align="center">You have <b>{{allowed - guessed}}</b> guesses left.</p> 
        </td> 
       </tr> 
       <tr> 
        <td> 

         <a custom-button>Click me</a> 
         <br /> 
         <button custom-button>Hello</button> 
        </td> 
       </tr>  
      </table> 
     </p> 
</div> 
</div> 
</body> 
</html> 

app.js

var gameApp = angular.module('guessGameApp', []); 
var gameTemplate = angular.module('guessGameApp', []); 

maincontroller.js

gameApp.controller("guessGameController", function($scope) 
    { 
    $scope.appTitle = "WELCOME TO THE GUESS GAME!"; 
    }); 

gameApp.controller('wordController', function($scope) 
    { 
    $scope.guess = ''; 
    $scope.guesses = []; 
    $scope.guessed= ''; 
    $scope.allowed = 6; 
    $scope.wordToGuess = "Just"; 

    $scope.pushGuess = function() { 
     $scope.guesses.push($scope.guestGuess); 
     $scope.guessed = $scope.guesses.length; 
     $scope.resetGuess(); 
    } 

    $scope.resetGuess = function() { 
     $scope.guestGuess = ''; 
    } 

    $scope.addGuess = function() 
     { 
     if ($scope.guestGuess == null || $scope.guestGuess == '') 
      { 
       $("input[type=text]").ready(function() { $("#guestGuess").addClass("blur"); }); 
       $scope.result = " Please enter a guess\n\nDon't leave the box empty."; 
       alert($scope.result); 
      } 
     else if ($scope.guestGuess.toLowerCase() == $scope.wordToGuess.toLowerCase()) 
      { 
       $("input[type=text]").ready(function() { $("#guestGuess").removeClass("blur"); }); 
       $scope.pushGuess(guestGuess); 
       $scope.result = "You have guessed the correct word. Way to go!\n\n\t\t  The word was: "; 
       alert($scope.result + $scope.wordToGuess); 
      } 
     else if ($scope.guestGuess != $scope.wordToGuess & ($scope.allowed - $scope.guessed) > 1) 
      { 
       $("input[type=text]").ready(function() { $("#guestGuess").removeClass("blur"); }); 
       $scope.pushGuess(guestGuess); 
       $scope.result = "Please try again!";  
       alert($scope.result); 
      } 
     else if (($scope.allowed - $scope.guessed) <= 1) 
      { 
       $("input[type=text]").ready(function() { $("#guestGuess").addClass("doneBlur"); }); 
       $scope.guesses.push($scope.guestGuess); 
       $scope.guessed = $scope.guesses.length; 
       $scope.result = "Game over! The word was: ";  
       alert($scope.result + $scope.wordToGuess); 
      } 
     $scope.guess = ''; 
     } 
    }); 
gameApp.directive('customButton', function() 
    { 
    $scope.wordToGuess = "Just"; 
    return { 
     restrict: 'A', 
     replace: true, 
     transclude: true, 
     templateUrl: '../../templates/customTemplate.HTML', 
     link: function (scope, element, attrs) 
      { 
      element.bind("click",function() 
       { 
       alert("The value of 'guessWord' is " + scope.wordToGuess); 
       }) 
      }}; 
    }); 

customTemplate.html

<a href="" class="myawesomebutton" ng-transclude> 
    <i class="icon-ok-sign"></i> 
</a> 
+0

Почему вы создаете два модуля с тем же именем в app.js? –

ответ

0

В app.js удаление второго объявлению модуля

var gameApp = angular.module('guessGameApp', []); 
//var gameTemplate = angular.module('guessGameApp', []); --> remove this line 

Вы также модифицирующие DOM от контроллера, это не угловой способ. Если вы хотите добавить классы при возникновении какого-либо условия, обратитесь к ng-class

+0

Себастьян, даже после удаления второго модуля моя директива кода не работает, я получаю следующую ошибку: 'Ошибка: $ scope не определен' – Tavo

+0

где вы это получаете? Убедитесь, что он правильно введен. В любом случае, теперь ваш контроллер вызывается? –

+0

Говорит мне, что в строке 61 моего контроллера эта строка «replace: true», что не имеет смысла. Но да, мой контроллер сейчас зовется. – Tavo

Смежные вопросы