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

标签 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;
}

最佳答案

您可能想要添加 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> 

当然,这需要你将right column重写成object。

关于javascript - AngularJS 使用 ng-focus 将列表项移动到列表顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44177678/

相关文章:

javascript - 试图了解这个 JS 函数的作用及其参数是什么,有人可以解释一下吗?

javascript - 更好地理解循环内的 JavaScript 闭包

angularjs - 为禁用按钮设置工具提示

javascript - 按小时分钟从最早到最晚的升序对对象数组进行排序

javascript - onClick 函数删除 :hover pseudo class

javascript - 如何在允许单指绘图的同时允许两指滚动 HTML Canvas 元素?

angularjs - 在 Angular4 中-如何添加 lodash 并进行无错误的构建

javascript - 如何使用 ui-router 将对象从 1 $state 发送到另一个 $state

javascript - $http.post 来自模态形式的数据( Angular )

javascript - 无法从 Controller 中的函数获取数据以进行 ng-repeat