2014-04-08 100 views
2

HTMLAngularJS替換列表時,點擊NG-點擊NG-重複

<ul class="feature-list-1"> 
    <li ng-repeat="(key, value) in features" ng-click="featureSelect(key)">{{key}}</li> 
</ul> 

ANGULARJS

$scope.features = 
{ 
    "administrative": [ 
     { id: 1, val: "Country"}, 
     { id: 2, val: "Province"}, 
     { id: 3, val: "Locality"}, 
     { id: 4, val: "Neighborhood"}, 
     { id: 5, val: "Land parcel"} 
    ], 
    "landscape": [ 
     { id: 1, val: "Man made"}, 
     { id: 2, val: "Natural"} 
    ] 
}; 

$scope.featureSelect = function(i) { 
    $('.feature-list li').remove(); 
    $.each($scope.features[i], function(i,v){ 
     $('.feature-list').append('<li>'+v.val+'</li>'); 
    }); 
} 

我學習AngularJS什麼,我想在這裏做的是當初始列表中的某個項目被點擊時,它將是administrativelandscape,我希望列表清除,然後用只有Angular的嵌套數據替換值。在示例Plunker我已經使用jQuery替換內容,並且我知道它效率不高,因爲我希望能夠正確地執行此操作。

例如。如果點擊landscape名單將清除,並通過Man madeNatural

Plunker

回答

5

這是我會怎麼做替代,具有綁定到中繼器另一個列表:

<ul class="feature-list-1"> 
    <li ng-repeat="(key, value) in features" ng-click="featureSelect(key)">{{key}}</li> 
</ul> 

<ul> 
    <li ng-repeat="feature in chosenFeatures">{{feature}}</li> 
</ul> 

現在的角:

$scope.featureSelect = function(key) { 
    $scope.chosenFeatures = $scope.feature[key]; 
} 

現在,當您選擇時,chosenFeatures範圍變量wil我會綁定到適當的列表並重復。簡單而乾淨。

+0

謝謝!有一些修補我能夠得到它的工作。非常感激! – ngplayground

+0

@Donald - 沒問題!一定要標記答案,如果它適用於亞! – tymeJV