获取所选值

时间:2017-01-02 07:01:06

标签: angularjs

<div ng-repeat="x in y">
<md-select ng-model="sampleName" placeholder="Name">
    <md-option  ng-value="{{ opt.Name }}" ng-repeat="opt in sampleJson">
          {{ opt.Name }}
    </md-option>                     
  </md-select>
</div>

需要获取许多生成的选择元素的所有选定值。

2 个答案:

答案 0 :(得分:1)

var app = angular.module('DemoApp', ['ngMaterial']);

app.controller('MainCtrl', function($scope) {
	
	$scope.options = [
	  {
	    name: 'Rome',
	    size: '200€',
	    image: 'http://lorempixel.com/120/60/cats/'
	  },
	  {
	    name: 'Naples',
	    size: '230€',
	    image: 'http://lorempixel.com/120/60/food/'
	  }
	  ];
	  
	  $scope.currOption = 	$scope.options[1];
	
	  $scope.updateData = function(){
	    $scope.options = [
    	  {
    	    name: 'London',
    	    size: '400€',
    	    image: 'http://lorempixel.com/60/60/abstract/'
    	  },
    	  {
    	    name: 'Paris',
    	    size: '900€',
    	    image: 'http://lorempixel.com/60/60/nature/'
    	  },
    	  {
    	    name: 'Rome',
    	    size: '200€',
    	    image: 'http://lorempixel.com/60/60/sport/'
    	  },
    	  {
    	    name: 'Naples',
    	    size: '230€',
    	    image: 'http://lorempixel.com/60/60'
    	  }
    	  ];
    	  
    	  $scope.currOption = 	$scope.options[1];
	  }
	
});
<!DOCTYPE html>
<html data-ng-app="DemoApp">

  <head>

    <!-- Angulars Material CSS using RawGit to load directly from `bower-material/master` -->
    <!--<link rel="stylesheet" href="https://rawgit.com/angular/bower-material/master/angular-material.css">-->
    <link rel="stylesheet" href="https://rawgit.com/angular/bower-material/5d70169b6147dc15144d3f85a929a9ac3f429584/angular-material.css">
    <script>
      document.write('<base href="' + document.location + '" />');
    </script>
        <!-- Angular Material Dependencies -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular-animate.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular-aria.js"></script>

    <!-- Angular Material Javascript using RawGit to load directly from `bower-material/master` -->
    <!--<script src="https://rawgit.com/angular/bower-material/master/angular-material.js"></script>-->
    <script src="https://rawgit.com/angular/bower-material/5d70169b6147dc15144d3f85a929a9ac3f429584/angular-material.js"></script>
    <script src="script.js"></script>
    <style>p { font-size: 0.75em; }</style>
  </head>
  <body data-ng-controller="MainCtrl">
    <h1>md-select demo</h1>
  	<!-- <select data-ng-model="currOption" data-ng-options="opt as opt.name for opt in options"></select> -->
  	<md-select data-ng-model="currOption">
  	  <md-select-label><img src="{{ currOption.image }}" /></md-select-label>
  		<md-option data-ng-value="opt" data-ng-repeat="opt in options"><img src="{{ opt.image }}" /></md-option>
  	</md-select>
  	</br></br></br>
  	{{currOption}}
  	
  	<md-button ng-click="updateData()">Change data</md-button>
  </body>
</html>

答案 1 :(得分:0)

<强>脚本

记住Angular JS在每个实例中更改变量的值,意味着当您控制该值时,它会显示它保存的最新值。

$scope.sampleName // Simiply go with this