1

I am new to web development, trying to learn angularjs, and got stuck at very first step, this code works fine when I remove ng-controller but in this condition the browser shows Hello, {{name}}.

What am I doing wrong?

index.html

  <!DOCTYPE html>
  <html lang="en" ng-app="app" ng-controller="AppCtrl" >
    <head >
        <script src="js/angular.min.js"></script>
        <script type="text/javascript" src="js/controller.js"></script>
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <meta character="uft-8">
    <title></title>
    </head>
    <body>
        <h1>Hello, {{name}}</h1>
        <input type="text" ng-model="name">
    </body>
  </html>

controller.js

  function AppCtrl($scope)
  {
    $scope.name: "world";
  }
1
  • Can you please provide what the question is and/or error message? Commented Dec 20, 2015 at 6:37

1 Answer 1

2

You are calling module named app which you didn't declared.

<html lang="en" ng-app="app" ng-controller="AppCtrl" >

If you provide module name then controller have to bind with that module.

To assign a value you have to use = not :

$scope.name: "world";

Try Like this

$scope.name= "world";

Moreover Global controller isn't allowed from 1.3.x

Try like this

var app = angular.module("app", []);
app.controller("AppCtrl", function($scope) {
   $scope.name= "world";
});

JSFIDDLE

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

3 Comments

Hi, thanks for replying, but I have tried doing this, it still doesn't work, what else could be the problem in this?
Hi, it works now, thank you very much, you the real MVP.
i read (yesterday) on official doc that making 'var app = angular.module("app", []); app.controller(..) ' is bad practice. better to do : 'angular.module("app", []); angular.module("app").controller();'

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.