根据下拉选项更改段落文本 - Angular

时间:2016-11-16 06:39:43

标签: javascript html angularjs ng-options

我正在尝试根据用户从下拉菜单中选择的内容更改字母中的段落,我无法让它工作。

我不确定ng-show / hide是否可行,或者ng-options是否可行。我在这一点上很失落。我整天都在绞尽脑汁。

app.controller('letterCreateCtrl', function($scope) {
$scope.selectItemsFilterCriteria = [
  {id: 1, name: "An event that occurred in the past 12 months"},
  {id: 2,name: "child/family got a new dog"},
  {id: 3, name: "child/family got a new cat"}
];

});
<select ng-options="item.name for item in selectItemsFilterCriteria" name="field_event">
    <option value="" disabled hidden selected>An event that occurred in the past 12 months</option>
     <optgroup label="Pets">
           <option value="Pets-1">Child/family got a new dog</option>
           <option value="Pets-2">Child/family got a new kitten</option>
           <option value="Pets-3">Child/family got a new rabbit</option>
     </optgroup>
   <optgroup label="Development">
     <option value="Development-1">Baby began sitting up on their own</option>
     <option value="Development-2">Child learned to walk</option>
     <option value="Development-3">Child started to get baby teeth</option>
   </optgroup>
</select>
  
  
<p>paragraph to change ipsum ipsum</p>

2 个答案:

答案 0 :(得分:1)

查看小提琴 - here

<div ng-controller="testCtrl">
   <select ng-options="item.name for item in selectItemsFilterCriteria" name="field_event" ng-model="paragraphToShow">
   </select>

<p>{{paragraphToShow.para}}</p>
</div>

答案 1 :(得分:1)

我认为你必须看看这个,可能是你找到解决问题的方法。

&#13;
&#13;
var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.flights = [{
    value: 'val1',
    text: 'text1'
  }, {
    value: 'val2',
    text: 'text2'
  }, {
    value: 'val3',
    text: 'text3 '
  }];
  $scope.model = {flight:'val2'}; 
  //this is a default value; if you don't want a default, you can leave this out,
  //and it will have a blank initially. otherwise, you can put a blank row and
  //handle it in validation
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.25/angular.js" data-semver="1.2.25"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
     <select name="flightNum" class="form-control" ng-model="model.flight" ng-options="v.value as v.text for v in flights" required></select>
     <br/>
     <br/>
     Selected value (in model.flight): 
     <div>
       <b>{{model.flight}}</b>
     </div>
  </body>

</html>
&#13;
&#13;
&#13;