2017-04-03 46 views
0
<body> 

<div ng-app="myapp"ng-model="user"ng-init="user=[{'name':'A','email':'[email protected]','mobile':'9494563132','city':'banglore'}, 
{'name':'B','email':'[email protected]','mobile':'9494563132','city':'pune'}, 
{'name':'C','email':'[email protected]','mobile':'9494563132','city':'hyderebad'}] "ng-init="fuser='hello'"> 
<div ng-controller="myctrl" > 
<table align="left"> 
<tr> 

<td><ul> 
    <h1>City</h1> 

    <li ng-repeat="x in user">{{x.city}}</li> 
</ul></td> 
</tr> 
</table> 

<table align="center"> 
<tr> 
<th>Select User</th> 
<td> 

<select ng-model="u" ng-change="getuser(name,email,mobile,city)"> 
    <option value="">Choose name</option> 
    <option ng-repeat="x in user" value="{{x.name}}">{{x.name}}</option> 

</select></td> 

</tr> 


<tr> 

    <table align="center" cellpadding="5" ng-model="table" bgcolor='skyblue'> 
    <caption><h2>User info</h2></caption> 
     <tr> 
     <th>Name</th> 
     <th>email</th> 
     <th>mobile</th> 
     <th>city</th> 
     </tr> 

     <tr ng-repeat="row in user|filter:fuser"> 
     <td>{{row.name}}</td> 
     <td>{{row.email}}</td> 
     <td>{{row.mobile}}</td> 
     <td>{{row.city}}</td> 
     </tr> 


</table> 
<tr> 
</table> 

</div> 

</div> 
<script> 
var obj = angular.module("myapp",[]); 
     obj.controller("myctrl",function($scope) 
      { 

        $scope.u=this.response;} 




       $scope.getuser=function(uname) 
       { 


        $scope.fuser=uname; 

       } 


      }); 

請幫幫我從數組中的數據提取到表通過使用下拉選項過濾數據和diplay所述定義的多維數組的數據傳遞到控制器相關data.i不能使用$ http事件,我不想在控制器初始化我的陣列如何從視圖中angularjs通過使用納克-INIT鑑於

回答

0

我記得你的要求,你不想把你的數據在控制器,但我不知道該怎麼做,所以我遵循常規程序....希望這會幫助你

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

 
app.controller('MainCtrl', function($scope) { 
 
    $scope.user=[{'name':'A','email':'[email protected]','mobile':'9494563132','city':'banglore'}, 
 
{'name':'B','email':'[email protected]','mobile':'9494563132','city':'pune'}, 
 
{'name':'C','email':'[email protected]','mobile':'9494563132','city':'hyderebad'}]; 
 

 
$scope.getuser=function(uname){ 
 
for(var i=0;i<$scope.user.length;i++){ 
 
    if($scope.user[i].name==uname){ 
 
    $scope.e=$scope.user[i]; 
 
    
 
} 
 
} 
 
} 
 
});
<!DOCTYPE html> 
 
<html ng-app="plunker"> 
 

 
    <head> 
 
    <meta charset="utf-8" /> 
 
    <title>AngularJS Plunker</title> 
 
    <script>document.write('<base href="' + document.location + '" />');</script> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script> 
 
    <script src="app.js"></script> 
 
    </head> 
 

 
    <body ng-controller="MainCtrl"> 
 
    <table align="left"> 
 
<tr> 
 

 
<td><ul> 
 
    <h1>City</h1> 
 

 
    <li ng-repeat="x in user">{{x.city}}</li> 
 
</ul></td> 
 
</tr> 
 
</table> 
 

 
<table align="center"> 
 
<tr> 
 
<th>Select User</th> 
 
<td> 
 

 
<select ng-model="x" ng-change="getuser(x)"> 
 
    <option value="">Choose name</option> 
 
    <option ng-repeat="x in user" value="{{x.name}}">{{x.name}}</option> 
 

 
</select></td> 
 

 
</tr> 
 

 

 
<tr> 
 

 
    <table align="center" cellpadding="5" ng-model="table" bgcolor='skyblue'> 
 
    <caption><h2>User info</h2></caption> 
 
     <tr> 
 
     <th>Name</th> 
 
     <th>email</th> 
 
     <th>mobile</th> 
 
     <th>city</th> 
 
     </tr> 
 

 
     <tr > 
 
     <td>{{e.name}}</td> 
 
     <td>{{e.email}}</td> 
 
     <td>{{e.mobile}}</td> 
 
     <td>{{e.city}}</td> 
 
     </tr> 
 

 

 
</table> 
 
</tr> 
 
</table> 
 
    </body> 
 

 
</html>

+0

謝謝大家的響應SAE,有沒有可能通過定義在我想知道的觀點@SaE – swapnika

+0

的NG-初始化指令陣列爲什麼你想從視圖中提取它從視圖中即提取數據,?從哪裏可以得到這些數據? – 2017-04-03 11:42:44