Abhishek Saxena Abhishek Saxena - 2 years ago 76
Javascript Question

$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>
<title>Using Angular JS Directives and Data Binding</title>

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

<!--<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>
<li data-ng-repeat="cust in customers | filter:name | orderBy:'city' ">{{ cust.name }} - {{cust.city}}</li>

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


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

Answer Source

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


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


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


Here is the working app

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download