2013-08-12 3 views
0

개체 배열을 반복적으로 반복합니다.ng-repeat 값의 동적 색인

ng-repeat에는 배열 인덱스로 동적 값을 사용해야하는 입력 모델 요소가 포함되어 있습니다. 아마 여기에 아주 명확하지 않은 설명이있을 것입니다.

<div ng-repeat="property in current_data.object_subtype.object_property_type" ng-init="set_input_state()" class="input-group ng-scope disabled"> 
    <span class="input-group-addon">{{property.name}}</span> 
    <input type="text" placeholder="{{property.name}}" ng-model="current_data.properties[getIndexFromId(current_data.properties, property.object_property_type_id)].value" class="form-control" disabled="disabled"> 

입력이 비어있는 것이 문제입니다. 나는 몇 가지 조합을 테스트 작업이를 발견했습니다

  1. getIndexFromId(current_data.properties, property.object_property_type_id) == 0
  2. current_data.properties[0].value는 예상 출력

그래서 어떻게 든 getIndexFromId(current_data.properties, property.object_property_type_id) 잘 각도에 의해 허용되지 않았거나 어딘가에 바보 같은 실수를 제공합니다. ..

이 문제가 무엇인지 아는 사람이 있습니까?

감사합니다.

내가 각도의 무엇을 본 적이에서
{ 
    "id": 1, 
    "name": "Robert Smith", 
    "object_subtype_id": 1, 
    "object_subtype": { 
    "id": 1, 
    "description": "Manager", 
    "object_property_type": [ 
     { 
     "id": 1, 
     "description": "Phone number" 
     }, 
     { 
     "id": 2, 
     "description": "Hair color" 
     }, 
     { 
     "id": 3, 
     "description": "Nickname" 
     } 
    ] 
    }, 
    "properties": [ 
    { 
     "id": 1, 
     "value": "819-583-4855", 
     "object_property_type_id": 1 
    }, 
    { 
     "id": 2, 
     "value": "Mauves", 
     "object_property_type_id": 2 
    }, 
    { 
     "id": 3, 
     "value": "Bob", 
     "object_property_type_id": 3 
    } 
    ] 
} 
+1

각 키에 대해 미리 지정된 인덱스가 있습니까? 그렇지 않다면'$ index'를 대신 사용해보십시오. – callmehiphop

+0

예, 인덱스가 데이터베이스에 저장됩니다. 지금은 1, 2, 3 등입니다.하지만 항상 그런 것은 아닙니다. –

+0

데이터 모델을 보여줄 수 있습니까? – zsong

답변

1

, 속성의 내용이 자바 스크립트로 실행되지 않습니다 여기

[편집]

이 모든 뒤에 데이터의 샘플입니다. 복잡한 인덱싱을 지원하지 않는 custom parsed and executed mini-language입니다.

그 말로는 아마도 최고 였을 것입니다. 컨트롤러 또는 서비스가 충분히 복잡한 논리를 처리해야합니다. 또한 실시간으로 모델을 업데이트 할 필요가없는 경우 ng-submit을 사용할 수 있습니다

<input type="text" placeholder="{{property.name}}" ng-model="property_name" ng-change="set_current_data_value(current_data, property)" class="form-control" disabled="disabled"> 

:

function MyController($scope) { 
    $scope.set_current_data_value = function (current_data, property) { 
     var index = $scope.getIndexFromId(current_data.properties, property.object_property_type_id); 

     current_data.properties[index].value = $scope.property_name; 
    } 
} 

그런 다음 HTML처럼 보일 것입니다.

+0

이 점을 지적 해 주셔서 감사합니다. 자바 스크립트로 파싱되었으므로이 점에 틀림 없습니다. 컨트롤러에서보다 복잡한 논리를 다루는 것이 더 합리적인 것이며, 잘 작동합니다. 고맙습니다! –