【问题标题】:AngularJS How to get selected value but not description of select values?AngularJS如何获取选定值但不是选择值的描述?
【发布时间】:2017-05-17 06:16:41
【问题描述】:

For drop downs in angular, I have select values and also descriptions in the dropdown, but when the value is selected, I'd like to only select the main values (cause).如何实现?

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('TypeaheadCtrl', function($scope, $http) {

  var _selected;

  $scope.selected = undefined;



  var url = 'https://spreadsheets.google.com/feeds/list/1O7M5gaEGlyE5gkOBARJrxJJMBYiTwz2THVjgbaTx9v8/od6/public/values?alt=json';
  var parse = function (entry) {
    console.log(entry);
    var description = entry.gsx$description.$t;
    var cause = entry.gsx$cause.$t;
    return {
       description: description,
       cause: cause
     };
  };
  $http.get(url)
    .then(function (response) {
      var entries = response.data.feed.entry;
      $scope.ready = true;
      $scope.parsedEntries = [];
      for (var key in entries) {
        var content = entries[key];
        $scope.parsedEntries.push(parse(content));		
      }
    });
});
<!doctype html>
<html ng-app="ui.bootstrap.demo">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-sanitize.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
    <div class='container-fluid typeahead-demo' ng-controller="TypeaheadCtrl">

      <h4>Static arrays</h4>
      <pre>Model: {{selected | json}}</pre>
      <input type="text" ng-model="selected" typeahead-editable="false" uib-typeahead="cause as cause.cause + ' - ' + cause.description for cause in parsedEntries | filter:$viewValue | limitTo:8" class="form-control">

    </div>
  </body>
</html>

如果 sn-p 不起作用,这是 plunker:https://plnkr.co/edit/aJzQCdX0izh5SnkJVgW3?p=preview

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    typeahead-on-select="onChange()" 添加到您的指令中:

    <input type="text" typeahead-on-select="onChange()" ng-model="selected" typeahead-editable="false" uib-typeahead="cause as cause.cause + ' - ' + cause.description for cause in parsedEntries | filter:$viewValue | limitTo:8" class="form-control">
    

    并在更改处理程序中获取cause 属性:

    $scope.onChange = function() {
      console.log($scope.selected.cause); 
    }
    

    【讨论】:

    猜你喜欢
    • 2015-05-09
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 2015-10-08
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    相关资源
    最近更新 更多