0

在我的單頁應用程序中,特別是在JavaScript文件中,我得到了重新捕獲函數的未捕獲引用的錯誤。我不明白是什麼導致了這個問題。我的js文件設置錯誤嗎? 所述的具體錯誤是,參考錯誤,重新定義沒有定義。AngularJS單頁面應用程序:引用錯誤,____未定義

HTML:

<body ng-app="app"> 
    <!-- == Main Controller for SPA == --> 
    <div class="container-fluid bg-dark text-white" style="height:700px" ng-controller="mainCtrl"> 
    <!-- == App Title == --> 
    <h2 class="display-2 title-container text-center">{{main.title}}</h2> 
    <div class="row"> 
     <div class="container-fluid word-container text-center"> 
     <!-- == User Phrase Input == --> 
     <input type="text" ng-model="word" placeholder="Please enter word"> 
     <br> 
     <br> 
     </div> 
    </div> 
    <div class="row"> 
     <div class="container-fluid anagram-container text-center"> 
     <!-- == Final anagram == --> 
     Anagram: {{anagram}} 
     </div> 
    </div> 
    <div class="row"> 
     <div class="container-fluid button-container text-center"> 
     <!-- Anagram "Reroll" Button --> 
     <button type="button" ng-click="reroll(word)" class="btn btn-primary btn-large">Reroll</button> 
     <!-- Anagram "Clear" Button --> 
     <button type="button" class="btn btn-primary btn-large" ng-click="word=''">Clear</button> 
     </div> 
    </div> 
    </div> 
</body> 

的Javascript:

var app = angular.module("app", []); 
app.controller('mainCtrl', function($scope) { 
    $scope.main = {}; 
    $scope.main.title = "AnagramJS"; 
    $scope.reroll = function reroll(value) { 
    // set anagram to randomize 
    $scope.anagram = value.split('').sort(function() { 
     return 0.5 - Math.random() 
    }).join(''); 
    }; 
    $scope.$watch('word', (newVal, oldVal) => { 
    if (newVal) { 
     reroll(newVal); 
    } else { 
     // empty anagram if word is empty 
     $scope.anagram = ""; 
    } 
    }); 
    angular.extend($scope, { 
    reroll 
    }); 
}); 
+0

我認爲你需要刪除'angular.extend ($ scope,{ reroll });' – Satpal

回答

0

reroll(newVal)應該$scope.reroll(newVal);

還能去除

angular.extend($scope, { 
    reroll 
}); 
+0

謝謝!有效。 – FreshOceans