1

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-

enter image description here

When I addd ng-controller="anything" as such...

<!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.

enter image description here

Can anyone please help, what I am doing wrong?

Thanks in advance for helping.

1
  • looks like you need to define a controller Commented Oct 19, 2015 at 23:40

2 Answers 2

1

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

And as always, refer to the Understanding Controllers docs for more information.

Sign up to request clarification or add additional context in comments.

Comments

1

in the first step you need to declare a name for your app

for exemple

Module.js

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

after that call this var in your controller

Controller.js

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

in your code html

index.html

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

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

2 Comments

why do you have a dependency of ngRoute in this? Also, what is tangibly different in your answer than the one I posted earlier?
only new information my friend cause if you separate the code in folder it make that easy to manage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.