2017-02-14 40 views
0

一直在AngularJS上工作,但我收到一個錯誤,指出Angular在被定義之前被使用。我觀看了一些視頻並閱讀了文檔。即使只是在js文件的第一行,我得到的錯誤。在索引文件中,我將angular 1.6.2作爲第一個文件之前的任何其他內容調用,這就是爲什麼我被困在爲什麼被告知在被定義之前使用它的原因。角度在定義之前使用

我的HTML是:

<!doctype html> 
<html ng-app="weatherApp"> 
    <head> 

    <!-- Bootstrap 3 Latest compiled and minified CSS --> 
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 

    <!-- AngularJS --> 
    <script src="//code.angularjs.org/1.6.2/angular.min.js"></script> 
    <script src="//code.angularjs.org/1.6.2/angular-route.min.js"></script> 
    <script src="//code.angularjs.org/1.6.2/angular-resource.min.js"></script> 
    <script src="app.js"></script> 

    </head> 
    <body> 
    <h1>Hello, world!</h1> 

     <div class="container"> 

      <div> 
      <div id="web-api"> 
       <h1>test</h1> 
      </div> 
      </div> 

     </div> 

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    </body> 
</html> 

和JS是:

// MODULE - WEATHER 
var weatherApp = angular.module('weatherApp', ['ngRoute', 'ngResource']); 

weatherApp.config(['$locationProvider', function($locationProvider) { 
    $locationProvider.hashPrefix(''); 
}]); 

// ROUTING 
weatherApp.config(function($routeProvider) { 
    $routeProvider 
    .when('/', { 
     templateUrl: 'home.htm', 
     controller: 'homeController' 
    }) 

}); 

weatherApp.controller('homeController', ['$scope', function($scope) { 

}]); 

weatherApp.controller('forecastController', ['$scope', function($scope) { 

}]); 
+1

我不確定它是否可以幫助,但是您是否嘗試將'ng-app'從'html'移動到'body'標籤? – Arkej

+0

你有沒有嘗試在你的'app.js'包括頭部而不是頭部? – UnholySheep

+0

在'angular.js'文件之前加載'angular-resource'和'angular-route'。將角度文件移動到jquery下面,這樣就可以在角度文件中使用jquey屬性 – nivas

回答

0

你使用任何服務器環境。請確保您在任何服務器環境(如live-server)下運行角路由。

還要確保擴展HTML文件被正確的路由配置在app.js

0

規定看起來沒有設置您的CDN正確。嘗試這些替代:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-route.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-resource.min.js"></script> 

而且根據docs,這是推薦的位置,並應提供更快的加載時間。

相關問題