2016-04-17 11 views
0

のhtmlコードに設定します。リストの最初の値をデフォルトの選択値として設定する必要がありました。最初の値をデフォルトのドロップダウン値を設定するために、デフォルトの値を

<div class="col-md-12" ng-controller="TestController as regCtrl" > 
<div ng-init="regCtrl.getMasterData()" > 
<select ng-model="regCtrl.user.screeningType.screeningTypeId" ng-change="regCtrl.getType(regCtrl.user.screeningType.screeningTypeId)" 
           ng-options="sctype.screeningTypeId as sctype.screeningType for sctype in regCtrl.screeningTypeList track by sctype.screeningTypeId" 
           class="form-control field-size ng-pristine ng-invalid ng-invalid-required ng-valid-maxlength ng-touched" name="screeningType" 
           id="screeningType" required > 
</div> 
</div>  

以下は、値を設定できるコントローラです。

(function() { 
    'use strict'; 
    angular.module('app.controllers') 
     .controller("TestController", TestController); 

    TestController.$inject = ['$http','LookupService','$filter', '$timeout', 'SweetAlertService', 'Constants']; 
    function TestController($http,LookupService,$filter, $timeout, SweetAlertService, Constants){ 
     var vm = this; 

         vm.getMasterData = function(){    
            $http({ 
       method: 'GET', 
       url: 'master/getAllScreeningTypeMast' 
      }).success(function(response){ 
       vm.screeningTypeList=response.result; 

      }).error(function(resp){ 

      }); 

      vm.user.screeningType.screeningTypeId = vm.user.screeningType.screeningTypeId[0].screeningTypeId; // getting undefined 
      } 
})(); 

これは

/** 
         [{ 
     "screeningTypeId": 2, 
     "screeningType": "Employment", 
     "activeYn": null, 
     "mmpidRequiredYn": "Y" 
    }, { 
     "screeningTypeId": 3, 
     "screeningType": "Female Marriage", 
     "activeYn": null, 
     "mmpidRequiredYn": "N" 
    }, { 
     "screeningTypeId": 4, 
     "screeningType": "Food", 
     "activeYn": null, 
     "mmpidRequiredYn": null 
    }, { 
     "screeningTypeId": 1, 
     "screeningType": "Male Marriage", 
     "activeYn": null, 
     "mmpidRequiredYn": "Y" 
    }] 

       */ 

がNG-INTをしようとしたサーバーからの応答で、NG-選択は何も働きました。

答えて

1

これを試して、.success関数で書く

vm.user.screeningType.screeningTypeId = response.result [0]。

ng-optionと同じように動作します。これはオブジェクト全体を格納します。 :) 私はあなたのに関連した迅速なコードを書かれている:

<!DOCTYPE html> 
<html> 

    <head> 
    <link rel="stylesheet" href="style.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script> 
    <script src="script.js"></script> 
    </head> 

    <body ng-app="app"> 
    <div class="col-md-12" ng-controller="TestController" > 
    <select ng-model="screeningTypeIdData" ng-options="op.screeningType for op in screeningTypeList track by op.screeningTypeId"> 

    </select> 
    </div> 

    </body> 

</html> 

と.jsファイル

コードをここに//

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

app.controller("TestController",['$scope','$http','$filter', '$timeout',function($scope,$http,$filter, $timeout) 
{ 



    $scope.screeningTypeList = [{ 
     "screeningTypeId": 2, 
     "screeningType": "Employment", 
     "activeYn": null, 
     "mmpidRequiredYn": "Y" 
    }, { 
     "screeningTypeId": 3, 
     "screeningType": "Female Marriage", 
     "activeYn": null, 
     "mmpidRequiredYn": "N" 
    }, { 
     "screeningTypeId": 4, 
     "screeningType": "Food", 
     "activeYn": null, 
     "mmpidRequiredYn": null 
    }, { 
     "screeningTypeId": 1, 
     "screeningType": "Male Marriage", 
     "activeYn": null, 
     "mmpidRequiredYn": "Y" 
    }]; 

$scope.screeningTypeIdData = $scope.screeningTypeList[0]; 
    // alert($scope.screeningTypeList[0].screeningTypeId); 
    //alert($scope.screeningTypeIdData) 

}]); 
+0

エラーが発生しました "angular.js:12477 TypeError:未定義の 'screeningTypeId'プロパティを設定できません" – user630209

+0

バインディング用としてng-modelを変更してfixを使用できますか? – user630209

+0

あなたはコードのフィドルを提供できますか? – SanchezCool

1

はあなたが設定する間違った変数を使用していませんあなたのデフォルト値ですか?代わりのと同様:

vm.user.screeningType.screeningTypeId = vm.user.screeningType.screeningTypeId[0]; 

それがあるべき:

vm.user.screeningType.screeningTypeId = vm.screeningTypeList[0]; 

vm.screeningTypeListする結果を含む配列です。 angular ng-options with preselected value by object id

これはあなたのng-optionstrack byによって引き起こされます。また、代わりにあなたがオブジェクト全体に

それを設定しなければならないだけでIDに変数を設定するので、ここで同様の問題が発生しました。

+0

12477 TypeError:定義されていないプロパティ 'screeningTypeId'を未定義の " – user630209

+1

@ user630209"に設定することはできません。おそらく、あなたが 'screeningTypeId'プロパティを存在しないオブジェクト(' vm.user.screeningType')変数を 'vm.screeningTypeId'だけにするか、値を設定する前に親オブジェクトを最初に作成してください:' vm.user = {screeningType:{}} ' –

0
vm.user.screeningType.screeningTypeId = vm.user.screeningType.screeningTypeId[0].screeningTypeId; 

上記のコードは、外部の.success機能で使用してください。

関連する問題