0

I am following the tutorial : AngularJS in 60 min and I am failed to get working a simple script where the controller is declared on the same page as the view (Indeed it is very basic). But actually it is not working properly:

So my code is :

<!DOCTYPE html>
<html data-ng-app="">
<head>
    <title>AngularJS in 60 minutes</title>
    <script src="./js/angular.min.js" charset="utf-8"></script>
</head>
<body>
<div class="container" data-ng-controller="SimpleController">
    <h3>Instance of model and data binding</h3>
    Name : <input type="text" data-ng-model="city"> {{ city }}

    <h3>Instance of repeat directives</h3>
    <ul>
    <ol data-ng-repeat="person in people | filter:city | orderBy:'city'">{{ person.firstname}} {{ person.name | uppercase}} : {{ person.city}}</ol>
    </ul>
</div>
    <script>
        function SimpleController($scope) {
            $scope.people = [{firstname:'Valerie', name:'lion', city:'Paris'}, {firstname:'Fred', name:'creche', city:'Marly'}, {firstname:'Laurent', name:'larher', city:'Massy'}];

        }
    </script>
</body>

and the result is in the image enclosed.

enter image description here

Any suggestions is welcome.

Fred

2

3 Answers 3

4

I suspect you are using Angular 1.3 or higher. As of 1.3, Angular no longer looks for controllers on window. Here is the migration link:

https://docs.angularjs.org/guide/migration

Instead, you should use the app.controller() syntax:

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

myApp.controller('SimpleController',function($scope) {
    $scope.people = [{firstname:'Valerie', name:'lion', city:'Paris'}, {firstname:'Fred', name:'creche', city:'Marly'}, {firstname:'Laurent', name:'larher', city:'Massy'}];
});

And change your html:

<html data-ng-app="myApp">
Sign up to request clarification or add additional context in comments.

1 Comment

Nah, it happens. Takes far too long to get up the Angular learning curve. FYI, I have a jsfiddle all set up with Angular I use for any of these issues. jsfiddle.net/9809ypgx
2

In your script you must define angular module and then controller:

angular.module('myApp', []).controller('SimpleController', ['$scope', function($scope) {
    // to do something....
}])

and in your HTML:

<html data-ng-app="myApp">
    ....
</html>

Comments

2

Since 1.3 you can't define your controller as a global function. You have to register in on a module. For this, you'll have to have a named module (data-ng-app='app'). Then, you'll be able to write

angular.module('app', []).controller("SimpleController", function($scope) { ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.