2016-11-20 34 views
1

我需要做出一個網頁應用,而無需像app.js的任何其他文件或controller.js角控制器直接的index.html

我創建了下面的例子,但我得到的錯誤: Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title> JavaScript Angular</title> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"> 
 

 
     var myАpp = angular.module("myАpp", []); 
 

 
     myАpp.controller("firstController", function($scope){ 
 
      $scope.test = "test-to-test"; 
 

 
     }); 
 

 
    </script> 
 
</head> 
 
<body ng-app = "myApp"> 
 
    <div ng-controller = "firstController"> 
 
     {{test}} 
 
    </div> 
 

 
</body> 
 
</html>

那麼,是不是可以有隻角一個index.html文件,仍然有內部控制器?

+0

簡單exampe內的控制器和模塊:http://www.w3schools.com/angular/tryit.asp?filename=try_ng_controller –

+0

然而,你的不工作的真正原因是你的'myApp'變量中有一個非ASCII字符。糾正這一點,並調整腳本標籤如下所示。 – Toddsden

回答

3

你需要有<script>標籤

<div ng-app="myАpp"> 
 
    <div ng-controller="firstController"> 
 
    <h1>{{test}}</h1> 
 
    </div> 
 
</div> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> 
 
<script> 
 
var app = 
 
    angular.module('myАpp', []).controller('firstController', function($scope){ 
 
    $scope.test = "test-to-test"; 
 
}); 
 
</script>

+0

我想你的意思firstController應該是testController – Deep

+0

@Deep是編輯 – Sajeetharan