简体   繁体   English

Angular.JS-添加控制器时出错

[英]Angular.JS - Error on adding Controller

I am new in . 我是 What I am trying to do is adding a simple controller in an angular app. 我想做的是在角度应用程序中添加一个简单的控制器。 So, my code is like this- 所以,我的代码是这样的-

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <div ng-app="">
            <div ng-init="mySwitch=true">
                <p>
                <button ng-disabled="mySwitch">Click Me!</button>
                </p>
                <p>
                <input type="checkbox" ng-model="mySwitch"/>Button Disable
                </p>
                <p>
                {{ mySwitch }}
                </p>
            </div>
        </div>
    </body>
    <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</html>

So, it is working perfectly- 因此,它运行得很好-

在此处输入图片说明

When I addd ng-controller="anything" as such... 当我这样添加ng-controller="anything" ...

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <div ng-app="">
            <div ng-controller="anything" ng-init="mySwitch=true">
                <p>
                <button ng-disabled="mySwitch">Click Me!</button>
                </p>
                <p>
                <input type="checkbox" ng-model="mySwitch"/>Button Disable
                </p>
                <p>
                {{ mySwitch }}
                </p>
            </div>
        </div>
    </body>
    <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</html>

Nothing seems to work. 似乎没有任何作用。

在此处输入图片说明

Can anyone please help, what I am doing wrong? 谁能帮忙,我做错了什么?

Thanks in advance for helping. 在此先感谢您的帮助。

Looks like you have not defined a controller. 看起来您尚未定义控制器。 Observe the following... 注意以下几点...

<div ng-app="app">
    <div ng-controller="ctrl">
[...]

angular.module('app', []).controller('ctrl', function($scope) {
    $scope.mySwitch = true;
});

JSFiddle Link - working demo JSFiddle Link-工作演示

And as always, refer to the Understanding Controllers docs for more information. 与往常一样,请参阅了解控制器文档以获取更多信息。

in the first step you need to declare a name for your app 在第一步中,您需要为您的应用声明一个名称

for exemple 举个例子

Module.js Module.js

var home = angular.module("home",['ngRoute']);

after that call this var in your controller 之后,在您的控制器中调用此var

Controller.js Controller.js

home.controller('homeController',function($scope){
  $scope.switch = value;
});

in your code html 在您的代码html中

index.html 的index.html

<!DOCTYPE html>
<html ng-app="home">

<div ng-controller="homeController" >
 {{switch}}
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM