2016-04-03 83 views
0

對於AngularJS我幾乎是新手。事實上,這是我的第一天。我在這裏要做的是從我製作的控制器獲取數據並在視圖中顯示它。但我不知道爲什麼,這不是簡單的工作。無法從控制器中獲取Angular.js中的數據

我的資料是學生名單。我所要做的只是按列表順序顯示學生列表,並根據在文本框中輸入的姓名過濾列表。

我的代碼非常簡單:

<!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> 

這裏有一個fiddle

回答

2

您需要註冊您的控制器!

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

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

然後您還需要將名稱放入ng-app

<html ng-app="myApp"> 
+0

謝謝。我不知道這件事 –

1

我已經更新您的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> 

角:

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'} 
       ]; 
}); 
1

您需要定義一個角模塊:

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

請參閱jsFiddle

相關問題