Scope variable not binding in view

I'm not sure why this scope variable is not binding in my view.

HTML:

<body ng-app="example">
  <div ng-controller="DemoCtrl">
    <h1>Hello {{name}}</h1>
  </div>
</body>

JS:

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

app.controller('DemoCtrl', function() {
  $scope.name = 'World';
});

Here is a JSbin with the example.


Update: 03/21/16

As @Dsafds noted in his answer, I simply forgot to include the $scope parameter. Wish AngularJS had a lint tool!

Answers


Because you forgot to import $scope:

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

app.controller('DemoCtrl', ["$scope", function($scope) {
  $scope.name = 'World';
}]);

So it would work then..

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

 app.controller('DemoCtrl', ["$scope",
   function($scope) {
     $scope.name = 'World';
   }
 ]);
<head>
  <script src="https://code.angularjs.org/1.4.9/angular.js"></script>
</head>

<body ng-app="example">
  <div ng-controller="DemoCtrl">
    <h1>Hello {{name}}</h1>
  </div>
</body>

You did not use $scope as a parameter in the controller:

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

DemoCtrl.$inject = ['$scope']; // <----- Add annotation
app.controller('DemoCtrl', function DemoCtrl($scope) { //<------- Add $scope here
  $scope.name = 'World';
});

See the working Jsbin.


pass $scope as dependency in controller function.

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

app.controller('DemoCtrl', function($scope) {
  $scope.name = 'World';
});

Need Your Help

Pass a 2d numpy array to c using ctypes

python c numpy ctypes

What is the correct way to pass a numpy 2d - array to a c function using ctypes ?

C Segmentation Fault in printf - dl-lookup.c

c multithreading segmentation-fault

I'm getting a seg fault while running a C multi-threaded code.