A
A
AlexanZem2015-03-01 10:48:05
Angular
AlexanZem, 2015-03-01 10:48:05

Why does the code in curly braces stop initializing when adding ng-controller to div?

Here is the code: as soon as I add ng-controller to the div, the code in {{}} such brackets does not work. name is not initialized, and for the 1+7 check, it is printed like that. Tell me what's the problem?

<!doctype html>
<html lang="en" ng-app>
<head>
  <meta charset="utf-8">
  <title>My HTML File</title>
  <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
  <link rel="stylesheet" href="css/app.css">
  <script src="bower_components/angular/angular.js"></script>
  <script>
  var DemoCtrl = function($scope){
    $scope.name = 'world';
  }
  </script>
</head>
<body>
  <div ng-controller='DemoCtrl'>
    <p>Hello {{name}}</p>
    <p>1 + 7 = {{1+7}}</p>
  </div>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kostya Nechaev, 2015-05-15
@kos403

You need to describe the controller as mentioned earlier.

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

myApp.controller('MyCtrl', function ($scope) { 
    ...
});

After that, in order for the code to work, you need to add the name of your application to ng-app.
In this case it will be
ng-app="myApp"

D
Dmitry, 2015-03-01
@thewind

Is this a normal declaration of the controller itself? Otherwise, it's necessary. And where is your ng-app directive?

D
DigitalSmile, 2015-03-02
@DigitalSmile

There is no controller declaration code. Should be something like:

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

myApp.controller('MyCtrl', function ($scope) { 
    ...
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question