0

我是一個新手的角度JS,發現了一個奇怪的疑難問題無法運行下面的代碼:角JS:控制器語法

hello.html的

<html ng-app> 
<head> 
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> 
    <script src="controller.js"></script> 
</head> 
<body> 
    <div ng-controller='HelloController'> 
    <p>{{greeting.text}}, World</p> 
    </div> 
</body> 
</html> 

controller.js

function HelloController($scope) { 
    $scope.greeting = { text: 'Hello' }; 
} 
+2

http://stackoverflow.com/questions/26646941/getting-an-error-when-使用-NG控制器功能於angularjs-VER-1-3-0/26647015#26647015 –

回答

1

Angular 1.3+不再支持全局範圍的控制器聲明。修改代碼爲

angular.module('app', []) 
.controller('HelloController', function ($scope) { 
    $scope.greeting = { 
     text: 'hello' 
    } 
}); 
1

你需要創建一個模塊控制器,例如:

angular.module('myApp.controllers') 
    .controller('HelloController', function ($scope) { 
     $scope.greeting = { text: 'Hello' }; 
    } 
});