Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular stops working When i use ng-controller in the html tag [duplicate]

I am new in Angular.js and start learning angular on plunker.co. The problem is when I use ng-controller in any tag of html page , angular stops working. I mean {{ 4+4 }} display as it is just after the using of ng-controller.

this is code from Plunker.co

<html ng-app>

  <head>
    <script data-require="[email protected]" data-semver="1.3.15" 
    src="https://code.angularjs.org/1.3.15/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="MainController">
    <h1>Hello Plunker!</h1>
    {{8*8}}
{{message}}

  </body>

</html>

without ng-controller it displays 64 as output but with ng-controller is displays as it is like {{8*8}}. why it is happening. Please give some advice. thnks in advance. :)

script.js

var MainController = function($scope){
  $scope.message = "Hello World!";
}

edited Now message attirbute value is not displaying on page.

like image 969
msk Avatar asked Apr 21 '15 15:04

msk


3 Answers

First try to set a name for your app :

<html ng-app="myApp">

then set a name for your controller :

<body ng-controller="MainController">

and finally, all you need to do is to define your app :

myApp = angular.module('myApp', []);

and try this definition for your controller :

myApp.controller('MainController', function($scope) {
    $scope.message = "Hello World!";
});
like image 113
Khalid Avatar answered Nov 12 '22 20:11

Khalid


ng-controller expects a parameter that is the name of the controller defined in your angular code. Something like ng-controller=foo. You should read up more on how Controllers work

like image 36
David says Reinstate Monica Avatar answered Nov 12 '22 20:11

David says Reinstate Monica


I'm not sure if you can use in the AngularJS controllers without name. Please set a name for your controller and it should work.

like image 1
Pasha.Proton Avatar answered Nov 12 '22 20:11

Pasha.Proton