2013-11-20 2 views
4

내 페이지에서 데이터베이스에서 입력 태그를 동적으로 생성했습니다. 이러한 필드는 그렇게 볼 수 있었다 : 내 서비스 이후 키 - 값 형식이 필요AngularJS에서 사전 같은 모델 바인딩을 만드는 방법

$scope.customFieldsDictionary = [{ 
    "Key": "customField-Street", 
    "Value": "SomeStreet" 
}, { 
    "Key": "customField-Height", 
    "Value": "125" 
}, { 
    "Key": "customField-IsBlack", 
    "Value": "true" 
}, { 
    "Key": "customField-Car", 
    "Value": "volvo" 
}]; 

:

<input id='customField-Street' type='text' /> 
<input id='customField-Height' type='text' /> 
<input id='customField-IsBlack' type="checkbox" /> 
<select id='customField-Car'> 
    <option value="volvo">Volvo</option> 
    <option value="saab">Saab</option> 
</select> 

내가 키 - 값 형식으로 다음과 같은 바인딩 모델을 설정하는 방법을 찾아야 해당 형식의 사용자 정의 데이터를 허용하십시오.

질문 : 이 방법은 형식 같은 지정 사전에 입력 필드와 $scope.customFieldsDictionary 필드 사이의 바인딩 AngularJS와 설정 방법.

답변

4
<div ng-repeat="obj in customFieldsDictionary"> 

    <input ng-model="obj.Value" id='{{obj.Key}}' ng-if="obj.Key == 
    'customField-Street' || obj.Key == 'customField-Height'" type='text'/> 

    <input ng-model="obj.Value" id='{{obj.Key}}' ng-if="obj.Key == 
    'customField-IsBlack'" type="checkbox" /> 

    <select ng-model="obj.Value" id='{{obj.Key}}' ng-if="obj.Key == 
    'customField-Car'" ng-options="car for car in cars"></select> 
</div> 

컨트롤러 :

function ctrl($scope){ 
    $scope.cars = ["Volvo","Saab"]; 
    $scope.customFieldsDictionary = [{ 
     ... 
    }]; 
} 
관련 문제