2017-01-02 1 views
0

이름이 ng-repeat 인 목록이 있습니다. 나는이 이름을 가지고 그의 과목을 듣기위한 매개 변수로서 URL에 전달하고 싶다. 지금까지 내가 한 것은 이름을 전달하고 다른 API에서 주제를 가져올 수 있다는 것입니다.Angularjs의 첫 번째 값을 기준으로 두 번째 값 표시

<ul ng-repeat="person in persons"> 
    <li ng-init="pass(person.name)">{{person.name}}</li> 
    <li> 
     <select> 
      <option ng-repeat="(key, value) in choices">{{value}}</option> 
     </select> 
    </li> 
</ul> 

Controller.js

$scope.person = { 
    [ 
     { 
      "id": 1, 
      "name": "Mark" 
     }, 
     { 
      "id": 2, 
      "name": "Jakob" 
     } 
    ] 
} 

$scope.pass = function(name) { 
    $scope.name = name; 
    $http.get('url?name=' + $scope.name, {}) 
     .then(function(response) { 
      $scope.choices = response.data; 
     }, 
     function(errResponse) { 
      console.error('Error while fetching choices'); 
     }); 

} 

참고 :의 끝에서 person.name을 전달하는 get 메소드가있다 그러나 문제는 마지막 이름은 자신의 값으로 모든 드롭 다운을 대체한다는 것입니다 URL.

HTML에서 컨트롤러로 어떻게 전달 될 수 있습니까? 감사

+0

가'전체를 통과'객체 대신 person' :

<ul ng-repeat="person in persons"> <li ng-init="pass(person)">{{person.name}}</li> <li><select> <option ng-repeat="(key, value) in person.choices">{{value}}</option> </select></li> </ul> 

다음은 샘플 바이올린의 person.name'을 호출 한 다음'pass' 메소드에서'person.name'을 보냅니다. 약속이 성공하면 반환 된 응답에서'person.subject'를 업데이트하십시오. –

+0

정확한 사례를 업데이트했습니다. –

+0

아래 답변을 참조하십시오. –

답변

1

확인 컨트롤러 코드와 같은 :

$scope.persons = [{ 
     "id": 1, 
     "name": "Mark" 
    }, 
    { 
     "id": 2, 
     "name": "Jakob" 
    } 
]; 


$scope.pass = function(person) { 
    $scope.name = person.name; 
    $http.get('url?name=' + $scope.name, {}).then(function(response) { 
      person.choices = response.data; 
     }, 
     function(errResponse) { 
      console.error('Error while fetching choices'); 
     }); 

} 

그리고 당신의 HTML과 같은 : https://jsfiddle.net/Lt7aP/4019/

+0

작동하지 않습니다. 모든 곳에서 여전히 동일한 것을 보여줍니다. 목록에있는 마지막 이름. –

+0

위의 샘플을 시험해 볼 수 있습니다. https://jsfiddle.net/Lt7aP/4018/ –

+0

감사합니다.하지만이 방법은 정말 좋은 해결책은 아닙니다. 나는 얼마나 많은 이름을 가지고 있는지 알지 못한다. 나는 1000을 가질 수있다. 1000을 만들 수 없다. 인덱스가있는 작은 솔루션을 발견했지만 여전히 문제가 발생했습니다. –

관련 문제