我刚刚开始使用 Angular 的 VS2015 ASP.Net5。使用 Bower 检索到的 Angular 时出现错误:
Error: [ng:areq] Argument 'TextController' is not a function, got undefined http://errors.angularjs.org/1.4.3/ng/areq?p0=TextController&p1=not%20a%20function%2C%20got%20undefined
这是我的 html 代码:
<!DOCTYPE html>
<html ng-app>
<head>
<meta charset="utf-8"/>
</head>
<body ng-controller="TextController">
<p>{{SomeText}}</p>
<!--does not work-->
<script src="lib/angular/angular.js"></script>
<!--works-->
<!--<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"> </script>-->
<script>
function TextController($scope) {
$scope.SomeText = "Go";
}
</script>
</body>
当我使用 Google 提供的 Angular 时,它就像一个魅力。 下面是在 VS 中使用 Bower 生成的图片
我错过了什么?
更新 感谢 Claies,这是工作版本:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="utf-8"/>
</head>
<body ng-controller="TextController">
<p>{{SomeText}}</p>
<!--does not work-->
<script src="lib/angular/angular.js"></script>
<script>
angular.module('myApp', []).controller('TextController', function ($scope) {
$scope.SomeText = "Go";
});
</script>
</body>
</html>
最佳答案
在最新版本的 Angular 中,您的 Controller 不能是全局的,即使可以,您也应该尝试模块化您的设计。
声明一个模块:
angular.module('myApp', [])
并将 Controller 注册到模块上:
angular.module('myApp')
.controller('TextController', function() { /*...*/ })
最后,将模块添加到 ng-app
指令中:
<html ng-app="myApp">
关于angularjs - 使用 Bower 时,Angular javascript 无法在 Visual Studio 2015 asp.net 5 项目中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31671133/