AngularJS控制器未注册 [英] Angularjs controller is not registered

查看:157
本文介绍了AngularJS控制器未注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习角度知识并使此示例脚本正常工作.

I'm trying to learn angular and make this example script work.

app.js:

var myApp = angular.module("myApp", []);

(function (app) {
    app.controller("TodoListController", function () {
        var todoList = this;

        todoList.todos = [
            { text: "learn angular", done: true },
            { text: "build an angular app", done: false }];

        todoList.addTodo = function () {
            todoList.todos.push({ text: todoList.todoText, done: false });
            todoList.todoText = "";
        };

        todoList.remaining = function () {
            var count = 0;

            angular.forEach(todoList.todos, function (todo) {
                count += todo.done ? 0 : 1;
            });

            return count;
        };

        todoList.archive = function () {
            var oldTodos = todoList.todos;
            todoList.todos = [];

            angular.forEach(oldTodos, function (todo) {
                if (!todo.done) {
                    todoList.todos.push(todo);
                }
            });
        };
    });
})(myApp);

Index.cshtml:

Index.cshtml:

<!DOCTYPE html>
<html ng-app>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>

    <link href="content/Site.css" rel="stylesheet" type="text/css" />

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
    <script src="scripts/app.js"></script>
</head>
<body>
    <h2>Todo</h2>
    <div ng-controller="TodoListController as todoList">
        <span>{{todoList.remaining()}} of {{todoList.todos.length}} remaining</span>
        [ <a href="" ng-click="todoList.archive()">archive</a> ]

        <ul class="unstyled">
            <li ng-repeat="todo in todoList.todos">
                <label class="checkbox">
                    <input type="checkbox" ng-model="todo.done" />
                    <span class="done-{{todo.done}}">{{todo.text}}</span>
                </label>
            </li>
        </ul>

        <form ng-submit="todoList.addTodo()">
            <input type="text" ng-model="todoList.todoText" size="30" placeholder="add new todo here" />
            <input class="btn-primary" type="submit" value="add" />
        </form>
    </div>
</body>
</html>

不幸的是,发生以下错误:未注册名称为'TodoListController'的控制器."

Unfortunately the following error occurs: "The controller with the name 'TodoListController' is not registered."

非常感谢您的帮助.

推荐答案

您需要在html中注册"myApp"模块.在html的第二行中,替换为< html ng-app ="myApp">

You would need to register "myApp" module in your html. In the second line of your html, replace with this <html ng-app="myApp">

这篇关于AngularJS控制器未注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆