Sam
Sam

Reputation: 501

Controller not working in angularjs

Below code is not giving output as expected i:e Hello,World output: {{ greetings.text }}, world

Can anyone help me why its not displaying 'hello, world' instead where I am wrong

<!doctype html>
    <html ng-app>
    <head>
    <meta charset="utf-8">
    <title>Angular JS App 1</title>
    <script type="text/javascript" src="angular-v1.4.js"></script>
    <script type="text/javascript" src="controllers.js"></script>
    </head>

    <body>
        <div ng-controller='HelloController'> //controller
            <p>{{ greetings.text }}, World</p>
        </div>
    </body>
    </html>

script for controller

function HelloController($scope){
    $scope.greetings = {text : 'hello'};
}

Upvotes: 1

Views: 53

Answers (3)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

Global controller isn't allowed from 1.3.x

Try like this

var app = angular.module("app", []);
app.controller("HelloController", function($scope) {
    $scope.greetings = {
        text: 'hellow world'
    }
});

HTML

add module name

<html ng-app="app">

Upvotes: 4

Durgpal Singh
Durgpal Singh

Reputation: 11983

add module name in ng-app.. like

<div ng-app='app'>
</div>

like this

<script>
  angular.module('app', [])
    .controller('testCtrl', ['$scope', function($scope){
      $scope.test ="hello world";
    }])
</script>

plunker code here

Upvotes: 1

Vivek
Vivek

Reputation: 13298

You need to provide the value for ng-app directive.

Like

<html ng-app="myapp">

and you should have the angular module myapp defined in script.

Upvotes: 0

Related Questions