用AngularJS中的选定项替换文本

时间:2017-10-15 10:40:57

标签: javascript angularjs

我的HTML中有下拉列表:

<div class="dropdown">
        <button class="dropdown-toggle" type="button" data-toggle="dropdown" ng-model="yearName">Sort by year
          <span class="caret"></span></button>
          <ul class="dropdown-menu">
            <li ng-repeat="year in ctrl.getYear() | orderBy: videoYear">
              <input type="checkbox" ng-model="ctrl.filter[year]" id='year{{$index}}' class='chk-btn styled-checkbox' />
              <label for='year{{$index}}'>{{year}}</label>

            </li>
          </ul>
        </div>

标题是&#39;按年份排序&#39;现在但是我想改变一个项目,它按标题获取。所以,如果我选择1984年,标题应该是1984年。我怎么能这样做?

感谢。

1 个答案:

答案 0 :(得分:0)

请检查代码

&#13;
&#13;
 angular.module("sampleApp", [])
   .controller('sampleCtrl', function($scope) {
     var ctrl = this;
     ctrl.filter = {};
     ctrl.getYear = function() {
       return [2001, 2002, 2003]
     }
   })
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="sampleApp" ng-controller="sampleCtrl as ctrl">
  <div class="dropdown">
    <button class="dropdown-toggle" type="button" data-toggle="dropdown" ng-model="yearName">Sort by year
      <span class="caret"></span></button>
    <ul class="dropdown-menu">
      <li ng-repeat="year in ctrl.getYear() | orderBy: videoYear">
        <input type="checkbox" ng-model="ctrl.filter[year]" id='year{{year}}' ng-true-value="{{year}}" class='chk-btn styled-checkbox' />

        <label for='year{{year}}'>{{year}}</label>

      </li>
    </ul>
    {{ctrl.filter}}
  </div>
&#13;
&#13;
&#13;