Frank Fotangs Frank Fotangs - 6 months ago 24
AngularJS Question

Problems displaying view in angularJS

I just started AngularJS today; so I'm still very new to it. I have the following code:

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

<div class="container">
<h3>Looping with the ng-repeat Directive</h3>

<input type="text" ng-model="nameText"/>{{ nameText}}
<ul>
<li data-ng-repeat="cust in customers | filter:nameText | orderBy:'name'">{{ cust.name | uppercase }} - {{ cust.city}}</li>
</ul>
</div>
<script type="text/javascript" src="angular.min.js"></script>

<script type="text/javascript">
function SimpleController($scope){
$scope.customers=[
{name:'Frank Ben',city:'Bamenda'},
{name:'Brize Tankanon',city:'Marous'},
{name:'Brendaline M.',city:'Bafoussam'},
{name:'Alexander Bings',city:'Buea'}
];
}





When I run the above code, this is what I get:

Screenshot with the controller

when I remove the controller directive from the body tag, I get this:

Screenshot without the controller

I don't know where the problem is coming from. I wish to display those names and cities. I will be grateful for your help. Thanks!

Answer

Try to register controller in angularjs app using build in dependency injection, in other words:

<script type="text/javascript">
  var app = angular.module("app", []);

app.controller('SimpleController', ['$scope', function SimpleController($scope){
    $scope.customers=[
        {name:'Frank Ben',city:'Bamenda'},
        {name:'Brize Tankanon',city:'Marous'},
        {name:'Brendaline M.',city:'Bafoussam'},
        {name:'Alexander Bings',city:'Buea'}
    ];
}]);

  </script>

then change ng-app to ng-app="app".

Here is JSBin with working example: http://jsbin.com/ficozajena/1/edit?html,js,output