AngularJS使用ng-focus将列表项移动到列表顶部

时间:2017-05-25 10:02:33

标签: javascript angularjs

我正在使用AngularJS,当点击特定输入/焦点时,我希望与该输入关联的列表项移动到列表顶部(最好从顶部向下推)

我一直在尝试一些技巧,包括ng-focus和ng-if但是没有任何运气能让它正常工作。

非常感谢任何帮助。

Here is my fiddle

HTML          

     <label class="control-label">Motive :</label>
     <input type="number" ng-model="payment.cash" ng-focus="addItem()" />

     <br>

     <label class="control-label">Employment Type :</label>
     <input type="number" ng-model="payment.check" ng-focus="addItem()" />

     <br>

     <label class="control-label">Money Order :</label>
     <input type="number" ng-model="payment.money_order" />

</div>

<div class="oneHalf">
    <h2>Help Text</h2>

    <ul class="help-text">

      <li>
        <h3>Motive</h3>
        <p>What made you go online today? What spurred you into action? Cost, Replacing cover, Family, job change What did you see online that appealed to you?</p>
      </li> 

      <li>
        <h3>Employment Type</h3>
        <p>Self employed - explain net profit (taxable income) Contracts - renewable? how many time renewed? Cover only applies to end of contract</p>
      </li>

      <li>
        <h3>Budget</h3>
        <p>What can the client comfortably afford? Check against salary</p>
      </li>

      <li>
        <h3>Industry</h3>
        <p>Check the Trent watch list clack here</p>
      </li> 

     <li>
        <h3>Life Sum</h3>
        <p>There is no normal<br>
        Salary x years of dependancy<br>
        10 x Salary<br>
        Funeral - what impact will inflation have on the sum assured!</p>
      </li> 
    </ul>

JS:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
}

CSS

.oneHalf {
  width:50%;
  float:left;
}

ul, li {
  list-style:none;
  margin:0;
  padding:0;
}

.help-text {
  position:relative;
}

.test {
  background:red;
  display:block !important;
}

1 个答案:

答案 0 :(得分:1)

您可能想要添加ngAnimate,但基本方法可以使用自定义orderBy比较器: FIDDLE

var myApp = angular.module('myApp',[]);

MyCtrl = function($scope) {
$scope.focus == null;
$scope.texts = [{h3:'Motive',p:'What made you go online today? What spurred you into action? Cost, Replacing cover, Family, job change What did you see online that appealed to you?'},{h3:'Employment Type',p:'Self employed - explain net profit (taxable income) Contracts - renewable? how many time renewed? Cover only applies to end of contract'}];
$scope.compare = function(a,b){
return a.h3 == $scope.focus?-1:1
}
}

      <li ng-repeat="text in texts |orderBy:compare">
        <h3>{{text.h3}}</h3>
        <p>{{text.p}}</p>
      </li> 

当然,这需要您将右列重写为对象。