1

I just started using AngularJS 1.3. I am trying to get access to scope related property that I assigned inside a function. But it seems that there is something wrong. Can someone help me out please?

  <!DOCTYPE html>
<html data-ng-app="">
<head>
    <title>SimpleController</title>
</head>
<body>

    <div data-ng-controller="SimpleController">
        <ul>
            <li data-ng-repeat="emp in employees">
                {{emp.name}}
            </li>

        </ul>
    </div>
    <script src="angular.min.js"></script>
    <script>
        function SimpleController($scope) {

            $scope.employees = [
            {name : 'emp1', id : 'id1'},
            {name : 'emp2', id : 'id2'},
            {name : 'emp3', id : 'id3'}
            ];
        }
    </script>
</body>
</html>
4
  • which version of angular is this? you can't use anonymous functions as controllers in newer versions of angular, they must be properly declared as a module. Commented Jul 30, 2015 at 15:18
  • stackoverflow.com/questions/31671133/… Commented Jul 30, 2015 at 15:19
  • this syntax will definitely not work. you should read the current angular documentation on the correct way to instantiate a controller, or try to follow the example in the question I posted, or other answers like it. The syntax function(SimpleController($scope) ... is definitely from an out of date tutorial. Commented Jul 30, 2015 at 15:26
  • another similar answer here stackoverflow.com/questions/29149665/… Commented Jul 30, 2015 at 15:28

1 Answer 1

2

It seems that the problem was the syntax I have used is deprecated and is not compatible to Angular 1.3. Here is the right approach:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title>SimpleController</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body ng-controller="SimpleController">
        <ul>
            <li data-ng-repeat="emp in employees">
                {{emp.name}}
            </li>

        </ul>

    <script>
        angular.module('myApp', []).controller('SimpleController', function($scope){

            $scope.employees = [
            {'name' : 'emp1', 'id' : 'id1'},
            {'name' : 'emp2', 'id' : 'id2'},
            {'name' : 'emp3', 'id' : 'id3'}
            ];
        });
    </script>

Thank you all for your help!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks all for being so helpful!
Although in this case syntax is correct it may be a bit misleading. angular.module('myApp', []) actually creates a module AND returns it. So you can chain it with .controller( instantiation. However if you attempt to create another controller/service/filter etc. with a copy of this line you will recreate module and thus erase all previously declared providers. So it's better to better save the module in var app=angular.module('myApp', []) and re-use it later

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.