2016-07-27 81 views
0

我嘗試學習角度js。 我嘗試在HTML一個簡單的代碼這樣包含角度時出錯

<!DOCTYPE html> 
    <html lang="en" ng-app=""> 
     <head> 
      <meta charset="utf-8"> 
      <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <title>Title Page</title> 

      <link rel="stylesheet" href="bootstrap.min.css" > 
     </head> 
     <body> 
      <input type="text" ng-model="name"> 
      <div>hello{(name)}</div> 
      <!-- jQuery --> 
      <script src="https://cdnjs.com/libraries/angular.js/"></script> 
    </body> 
</html> 

結果當我在Web瀏覽器中運行它是一個錯誤。它沒有工作 在控制檯上的錯誤是這樣

Uncaught ReferenceError: System is not defined on line 2 

怎麼來的,我得到的錯誤也是這樣嗎?

+0

嘗試包括本腳本標記'' –

回答

0

不能包括所有的https://cdnjs.com/libraries/angular.js/頁,你需要選擇你想包括腳本,是這樣的:

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> 

而且,{{name}}(兩個花括號),而不是{(name)}

<!DOCTYPE html> 
 
    <html lang="en" ng-app=""> 
 
     <head> 
 
      <meta charset="utf-8"> 
 
      <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
      <title>Title Page</title> 
 
      
 
     </head> 
 
     <body> 
 
      <input type="text" ng-model="name"> 
 
      <div>hello {{name}}</div> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> 
 
    </body> 
 
</html>

0

這是指向AngularJS CDN頁數:

<script src="https://cdnjs.com/libraries/angular.js/"></script> 

您應該選擇一個版本。例如精縮版AngularJS 1.5.8:

<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> 

請注意,您應該排除https

另外檢查打印出AngularJS中的變量,你應該使用{{ name }}而不是{(name)}

0

這裏至少有兩點需要突出顯示。

  • 您的導入。其他答案正在完美地描述它爲什麼不起作用。修理它。

  • 角本身。角度作品應用程序控制器(爲其最簡單的部分)。你需要做的是製作一個包含你的控制器的文件。例如,創建一個名爲myapp.js文件,並將其導入到你的網頁:

<script src="path/to/myapp.js"></script>

然後,一旦你初始化你的應用程序(var myApp = angular.module('myApp',[]);會做很好的伎倆)和控制器(檢查AngularJS documentation about it) ,你就可以用你的範圍並初始化數據:

$scope.name = 'John';

打電話給你的應用程序在你的HTML頁面:

<html ng-app="myApp">

而且你可以看到神奇的工作有:

hello {{ name }} // and not {(name)}

Web瀏覽器應實際顯示:

hello John