0

I'm pretty much new to AngularJS. In fact, this is my first day. What I'm trying to do here is to fetch data from a controller I made and show it in the view. But I don't know why, it's not simply working.

My data is a list of students. All I'm trying to do is to show the list of students in a list order and filter the list according to the name entered in a textbox.

My code is pretty simple:

<!DOCTYPE html>
<html ng-app>

  <head>
    <script  src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
  </head>

  <body>
    <h1>Hello!</h1>

      Student Name:
     <br />
      <input type="text" ng-model="sname" /> {{ sname }} 
    <div id="mvvm_communication" class="container" data-ng-controller="simpleController">

      <ul>
        <li ng-repeat="stud in students | filter:sname  | orderBy:'firstname'" >{{stud.firstname | lowercase }}, {{stud.lastname| uppercase }}</li>
      </ul>

    </div>

    <script>
      function simpleController($scope)
      {
        $scope.students=[
               {firstname:'Jordan',lastname:'Rains'},
               {firstname:'Michael',lastname:'Jordan'},
               {firstname:'John',lastname:'Doe'},
               {firstname:'John',lastname:'Smith'},
               {firstname:'Simcha',lastname:'Michelle'},
               {firstname:'Sydney',lastname:'Rivers'},
               {firstname:'Summer',lastname:'Rose'},
               {firstname:'Georgia',lastname:'Schubert'},
               {firstname:'Rosalie',lastname:'Fayadh'}
              ];

      }      
    </script>    
  </body>
</html>

Here's a fiddle.

3 Answers 3

2

You need to register your controller!

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

myApp.controller('simpleController', ['$scope', simpleController]);

And then you also need to put a name into ng-app.

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

1 Comment

Thanks. I didn't knew about this
1

I've updated your JSFiddle:

HTML:

<div ng-app="app">
  <div ng-controller="simpleController">
    <h1>Hello Student!</h1>   Student Name:<br/>
    <input type="text" ng-model="sname" /> {{ sname }} 
    <div id="mvvm_communication" class="container">
      <ul>
        <li ng-repeat="stud in students | filter:sname  | orderBy:'firstname'" >{{stud.firstname | lowercase }}, {{stud.lastname| uppercase }}</li>
      </ul>
    </div>
  </div>
</div>

Angular:

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


app.controller('simpleController', function($scope) {

        $scope.students=[
               {firstname:'Jordan',lastname:'Rains'},
               {firstname:'Michael',lastname:'Jordan'},
               {firstname:'John',lastname:'Doe'},
               {firstname:'John',lastname:'Smith'},
               {firstname:'Simcha',lastname:'Michelle'},
               {firstname:'Sydney',lastname:'Rivers'},
               {firstname:'Summer',lastname:'Rose'},
               {firstname:'Georgia',lastname:'Schubert'},
               {firstname:'Rosalie',lastname:'Fayadh'}
              ]; 
});

Comments

1

you need to define an angular module:

      angular.module('app', []).controller('simpleController', simpleController);

see jsFiddle

Comments

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.