首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >根据单击值列表更改选项中选定的值

根据单击值列表更改选项中选定的值
EN

Stack Overflow用户
提问于 2016-02-04 11:15:32
回答 2查看 44关注 0票数 0

当单击select值列表时,我试图相应地更改select中的选定值。

我的第一个想法是试图将这两种模型结合在一起,但它并没有像我预期的那样起作用。

代码语言:javascript
运行
复制
(function(angular) {
  'use strict';
  angular.module('staticSelect', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.data = {
        options: [
          1, 2, 3, 4, 5, 6, 7
        ]
      };
      $scope.selectOption = function(option) {
        // To check that I get the correct value
        alert(option);
        // I want whenever I click on a value, it will toggle the select option box sync with that value
        $scope.option1 = option;
      }

    }]);
})(window.angular);
代码语言:javascript
运行
复制
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-static-select-production</title>
  

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.2/angular.min.js"></script>
  <script src="app.js"></script>
  

  
</head>
<body ng-app="staticSelect">
  <div ng-controller="ExampleController">
  <form name="myForm">
    <select name="" id="">
      <option value="option" ng-repeat="option in data.options" ng-model="option1">{{option}}</option>
    </select>
  </form>
  <div ng-repeat="option in data.options">
    <span ng-model="option2" ng-click="selectOption(option)">{{option}}</span>
  </div>
</div>
</body>
</html>

<!-- 
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-04 11:53:11

使用以下方法修改select语句

代码语言:javascript
运行
复制
 <select name="" id="" ng-model="option1">
  <option value="option" ng-repeat="option in data.options" ng-selected="option1 == option">{{option}}</option>
</select>

希望这能帮到你。

票数 1
EN

Stack Overflow用户

发布于 2016-02-04 12:16:01

你可以试试这个

代码语言:javascript
运行
复制
<select ng-options="c.Id as c.Name for c in data" ng-model="option1">

因此,在option1中,您将获得select项的IdController

代码语言:javascript
运行
复制
angular.module('staticSelect', [])
.controller('ExampleController', ['$scope', function($scope) {
  $scope.option1 = 1;
  $scope.data = [{"Id":1,"Name":"Theme1"},
                 {"Id":2,"Name":"Theme2"},
                 {"Id":3,"Name":"Theme3"}];
}]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35199564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档