2016-12-01 5 views
2

json()からデフォルト値を取得するはずですが、選択した項目のデフォルト値を設定できません。 。私のコントローラで選択されたngモデルのデフォルト値を設定して変更を管理

私が選択したこの方法を設定している:

.controller('attributeFacetCtrl', function ($scope, tabsService, $location, contentService, attribute) { 

    $scope.attribute = attribute; 

    $scope.types = [{ 
     val: 'terms' 
    }, { 
     val: 'continuous' 
    }]; 

    $scope.selected = $scope.attribute.facet.data.type; 

    $scope.isTerms = false; 

    $scope.validateFrom = function() { 

     var chk = $scope.selected.val; 

     if (chk === 'terms') { 
      $scope.isTerms = true; 
     } else { 
      $scope.isTerms = false; 
     } 
    }; 
}) 

$ scope.attribute.facet.data.type = "用語"

そして、私のビューIでの結果この

<div class="form-group"> 
         <label class="wk-field-label"> 
          <i class="icon-asterisk wk-prefield wk-mandatory"></i> 
          Type 
         </span> 
         </label> 
         <select class="wk-field-input" ng-model="selected" 
           ng-options="typeValue as typeValue.val for typeValue in types" 
           ng-change="validateFrom()" 
           required></select> 
        </div> 
        <div ng-if="isTerms" ng-repeat="orders in attribute.facet.data.order"> 
         <div> 
          <label class="wk-field-label"> 
           <i class="icon-asterisk wk-prefield wk-mandatory"></i> 
           Order 
           </span> 
          </label> 
        </div> 
</div> 
+0

'console.log($ scope.attribute.facet.data.type);の出力は何ですか? –

+0

console.log outpus:terms – kyserslick

答えて

1

問題は、あなたが文字列(「用語」)に$scope.selectedを設定しているが、あなたがng-optionsそれをロードしているときということである持っていますそのオブジェクトにはvalプロパティを持つオブジェクトが読み込まれます。

あなたは必ず、あなたは下記をごng-optionsを変更することができ、対象となる$scope.selectedを必要としない場合:

ng-options = "typeValue.val as typeValue.val for typeValue in types" 

あなたがvalプロパティを持つオブジェクトであることを$scope.selectedが必要な場合は、あなたが必要となりますその初期値を適切に設定し、それを文字列に設定してデフォルト選択を設定することはできません。あなたは文字列で要素を設定したいので

+0

私のng-optionsでtypeValue.valが正しく動作しています – kyserslick

0

、次のようにあなたのselect要素を変更:

<select 
    class="wk-field-input" 
    ng-model="selected" 
    ng-options="typeValue as typeValue.val for typeValue in types" 
    ng-change="validateFrom()" 
    required 
> 
</select> 

あなたがここにasを使用する方法の詳細を読むことができます:助けhttps://docs.angularjs.org/api/ng/directive/ngOptions

希望を!

+0

私の質問を更新しました_typeValue as_はそれを変更せず、デフォルトの選択値はまだ私のビューでは空です – kyserslick

+0

ハードコード '$ scope.selected'を '{val: 'terms'}'として作成し、 'terms 'としてハードコードしてみてください - その2人のうちの1人が動作しなければなりませんが、ハードコーディングは、少なくともそれらのうちの1つを削除します:) –

関連する問題