2016-06-16 8 views
0

もう一度基本的なAngular質問に戻ります。さて、ASP.Net MVCとAngularJSを使用して、データベースから読み込み、データを簡単に表示することができました。AngularJSを使用して複数の要素を複数の表にバインドする

ビュー:

<table class="table table-striped"> 
    <tr ng-repeat="p in products"> 
     <td>{{p.pName}}</td> 
    </tr> 
</table> 

角度コントローラー:

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

myApp.controller('mainController', function ($scope, $http) { 
$http.get('/Home/GetProducts') 
    .success(function (result) { 
     $scope.products = result; 
    }) 
    .error(function (data) { 
     console.log(data); 
    }); 
}); 

MVCコントローラ:

public JsonResult GetProducts() 
    { 
     var db = new testDBEntities(); 
     return this.Json((from pObj in db.Products 
          select new 
          { 
           pId = pObj.Id, 
           pName = pObj.nme, 
           pDesc = pObj.descr,         
          }) 
          , JsonRequestBehavior.AllowGet 
         ); 
    } 

私の質問は次のとおりです。 I以下の例では、1つの非常に簡単です私のビューに別の要素を追加したい選択タブを言う。そして、私はすべてのユーザーをリストすることができるusersテーブルにその要素をバインドしたいと思います。

私はこれを手伝ってもらえますか?これを行う方法は非常に混乱しています。私は運で次の操作を行うことを試みた

<select ng-model="getUsers"> 
    <option ng-options="x in users">{{x.ufName + " " + x.ulName}}</option> 
</select> 

//////

$scope.getUsers = function() { 
    $http.post('/Home/GetUsers') 
     .success(function (result) { 
      $scope.users= result; 
     }) 
     .error(function (data) { 
      console.log(data); 
     }); 

////

public JsonResult GetUsers() 
    { 
     var db = new testDBEntities(); 
     return this.Json((from userObj in db.Users 
          select new 
          { 
           uId = userObj.Id, 
           ufName = userObj.usrFirstName, 
           ulName = userObj.usrLastName,         
          }) 
          , JsonRequestBehavior.AllowGet 
         ); 
    } 
+0

推奨されていません $ http従来の約束方法の成功とエラーは廃止されました。代わりに標準の**、**を使用してください。 – Manatax

+0

私は理解できませんでした。私に例を挙げてください。 –

+0

$ scope.getUsers =関数(){ $ http.post( '/ホーム/ GetUsers') .then( 関数(結果){ $ scope.users =結果; }、 機能(データ){ (データ); }); – Manatax

答えて

0

はng-を入れてみてください選択肢ではなく、オプションではなく私がコメントで言っクリアする

<select ng-options="item as item.label for item in items track by item.id" ng-model="selected"></select> 

resource

EDIT。

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

myApp.controller('mainController', function ($scope, $http) { 
    $http.get('/Home/GetProducts').then(
     function (result) { 
      // success 
      $scope.products = result; 
     }, 
     function (data) { 
      // fail 
      console.log(data); 
     } 
    ); 

    $http.get('/Home/GetUserss').then(
     function (result) { 
      // success 
      $scope.users = result; 
     }, 
     function (data) { 
      // fail 
      console.log(data); 
     } 
    ); 
}); 
+0

私が例を挙げてくれれば非常に感謝しています。私は運がないとこれを試しました –

+0

私の角度コントローラを見れば、MVCコントローラを取得するコードは関数の中にあります。 –

+0

@AhmedAlsahliはリソースのリンクに従っています。 – Manatax

関連する問題