Abhishek Saxena
Abhishek Saxena

Reputation: 292

$scope is coming as undefined

I have started learning angular js. I wanted to bind my view through custom controller. But on debugging I got scope undefined.Here is my code

<!DOCTYPE html>
<html data-ng-app>
<head>
    <title>Using Angular JS Directives and Data Binding</title>

</head>
    <body data-ng-controller='SimpleController'>
        Name: <br/>
        <input type="text" data-ng-model="name"/>
        <br/>

        <!--<div class="container" data-ng-init="customers=[{name:'Abhishek',city:'Bangalore'},{name:'Sagar',city:'New York'},{name:'Jarvis',city:'Atlanta'}]" >-->
        <div class="container">
            <h3>looping using ng-repeat Directive</h3>
            <ul>
                <li data-ng-repeat="cust in customers | filter:name | orderBy:'city' ">{{ cust.name }} - {{cust.city}}</li>
            </ul>
        </div> 

        <script src="angular.min.js"></script>
        <script>
            function SimpleController($scope){
                $scope.customers = [
                    {name :'Abhishek',city:'Bangalore'},
                    {name:'Sagar',city:'New York'},
                    {name :'Jarvis',city:'Atlanta'}
                ];
            }
        </script> 

    </body>
</html>

Getting this error. Argument 'SimpleController' is not a function, got undefined

Upvotes: 0

Views: 56

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222532

You have not declared angular module and ng-app anywhere in your application.

HTML:

<body ng-app="app"  ng-controller='SimpleController' >

JS:

  var app = angular.module('app', []);
    app.controller('SimpleController',['$scope',function($scope)
    {
       $scope.customers = [
                        {name :'Abhishek',city:'Bangalore'},
                        {name:'Sagar',city:'New York'},
                        {name :'Jarvis',city:'Atlanta'}
                    ];

    }]);

Here is the working app

Upvotes: 1

Related Questions