2014-10-19 79 views
0

我對Angularjs非常陌生,我將列名(鍵值對中的鍵)綁定到了選擇列表並希望得到選擇變更選擇的關鍵名稱。 選擇清單顯示:如果下拉列表綁定了json的鍵,如何從下拉列表中選擇值

名稱,摘要和年齡

並選擇更改我想要的選擇選定的文本。請幫助 這裏是我的代碼:

<!DOCTYPE html> 

<div ng-controller="HelloController"> 

    <h2>Hello {{helloTo.title}} !</h2> 
    <select ng-options="key for (key,val) in phones[0]" ng-change="ChangeValue(key)" ng-model="phones"></select> 
</div> 


<script> 
    angular.module("myapp", []) 
     .controller("HelloController", function ($scope) { 
      $scope.helloTo = {}; 
      $scope.helloTo.title = "World, AngularJS"; 
      $scope.phones = [ 
       { 
        'name': 'Nexus S', 
        'snippet': 'Fast just got faster with Nexus S.', 
        'age': 1 
       }, 
       { 
        'name': 'Motorola XOOM™ with Wi-Fi', 
        'snippet': 'The Next, Next Generation tablet.', 
        'age': 2 
       }, 
       { 
        'name': 'MOTOROLA XOOM™', 
        'snippet': 'The Next, Next Generation tablet.', 
        'age': 3 
       } 
      ]; 
      $scope.ChangeValue = function (selectedval) { 

       alert(selectedval.name); 
      }; 
     }); 
</script> 

+0

即時通訊不是非常專業,但如果您控制選定的Val它輸出什麼? – ProllyGeek 2014-10-19 09:48:51

+0

它返回undefined作爲值 – Gkhanna 2014-10-19 09:49:54

+0

並提醒工作,我的意思是ChangeValue方法觸發? – ProllyGeek 2014-10-19 09:50:57

回答

1
<select ng-options="key as key for (key,value) in phones[0]" ng-change="ChangeValue()" ng-model="selectedVal"></select> 

而且控制器:

angular.module("myapp", []) 
     .controller("HelloController", function ($scope) { 
$scope.helloTo = {}; 
    $scope.helloTo.title = "World, AngularJS"; 
    $scope.selectedVal = ''; 
    $scope.phones = [{ 
    'name': 'Nexus S', 
    'snippet': 'Fast just got faster with Nexus S.', 
    'age': 1 
    }, { 
    'name': 'Motorola XOOM™ with Wi-Fi', 
    'snippet': 'The Next, Next Generation tablet.', 
    'age': 2 
    }, { 
    'name': 'MOTOROLA XOOM™', 
    'snippet': 'The Next, Next Generation tablet.', 
    'age': 3 
    }]; 
    $scope.ChangeValue = function() { 

    console.log($scope.selectedVal); 
    }; 
}); 

它的權利在實際documentation ...

這裏有一個plunker

+0

實際上,我的下拉菜單顯示的列名不是像「名稱,代碼段和年齡」的值,而是一次我點擊任何項目,我想這個列名不是名稱/片段/電話的年齡。所以請更新您的答案 – Gkhanna 2014-10-19 12:32:14

+0

檢查我的編輯。這是你想要的還是我誤解了? – 2014-10-19 12:44:55

+0

部分正確,一旦我選擇「名稱」,它會記錄「Nexus S」,因爲我想要「名稱」本身。你能否更新? – Gkhanna 2014-10-19 13:00:05