angularjs - 使用 Bower 时,Angular javascript 无法在 Visual Studio 2015 asp.net 5 项目中工作

标签 angularjs bower visual-studio-2015 asp.net-core

我刚刚开始使用 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 生成的图片

partial project view

我错过了什么?

更新 感谢 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/

相关文章:

angularjs - 单选按钮 ng-checked with ng-model

php - 如何将 node_modules 文件夹部署到生产服务器?

angularjs - Grunt Wiredep 没有在 Yeoman Angular 教程中注入(inject) Bootstrap css 文件

c# - 将 project.json 包引用添加到 VSIX

html - 如何使用 Angular 将单元格 colspan 基于条件值?

javascript - Angular js ng-repeat不起作用

javascript - 在 Angular 中集成 Brightcove 播放器

javascript - Bower 将依赖项安装到特定子目录

visual-studio - 诊断工具窗口不支持使用 'Script' 调试引擎进行调试

visual-studio - 代码定义窗口在 VS2015 中不起作用