Angular JS根据搜索结果和点击事件更新DOM元素

Angular JS update DOM element based on search results and click events

本文关键字:事件 更新 DOM 元素 JS 搜索结果 Angular      更新时间:2023-09-26

我有个问题。我制作了一个有角度的web应用程序,它涉及到对json格式日期的ajax调用,以在表中显示它们。它有一个搜索输入,两个按钮(按年龄或姓名排序)。它还有一个配置文件(边栏元素),

我已经学会了如何更新点击表行上的边栏(姓名、年龄、图片、短语、最喜欢的动物),但当我按下排序按钮时,我需要更新边栏信息(按姓名或年龄排序,当我进行搜索输入时)。我必须根据点击的排序按钮或搜索输入,以某种方式用json数据填充边栏。非常感谢!

这是html部分

<div class="app container-fluid"  ng-controller="mainController">
    <form>
      <div class="form-group">
        <div class="input-group">
          <div class="input-group-addon"><i class="fa fa-search"></i></div>
          <input type="text" class="form-control" placeholder="Search right person" ng-model="searchAnimal" ng-model="updateAnimal">
        </div>      
      </div>
    </form>
    <div class="row">
      <div class="col-sm-12">
        <div class="toolbar">
          <button ng-click="sortType = 'name'; sortReverse = !sortReverse; clickAnimal()" class="btn btn-default"><i class="icon fa fa-sort-alpha-asc"></i><span>  Sort by name</span>
            <span ng-show="sortType == 'name' && !sortReverse"></span>
          </button>
          <button ng-click="sortType = 'age'; sortReverse = !sortReverse; clickAnimal()" class="btn btn-default"><i class="icon fa fa-sort-numeric-desc"></i><span>  Sort by age</span>
            <span ng-show="sortType == 'age' && !sortReverse"></span>
          </button>
        </div>
      </div>
    </div>
    <div class="row" ng-cloak>
      <div class="col-sm-4 col-md-3 col-lg-2">
        <div class="thumbnail"><img ng-src="{{ animImage }}.svg"/>
          <div class="thumbnail-caption"><h3>{{ animName }}</h3><table class="user-info table table-responsive" ><tbody><tr><td>Age:</td><td>{{ animAge }}</td></tr><tr><td>Favorite animal:</td><td>{{ animImage }}</td></tr><tr><td>Phone:</td><td><span>{{ countryCode }} </span>{{ animPhone }}</td></tr></tbody></table><p><b >Favorite phrase:</b><span> </span><span>{{ animPhrase }}</span></p></div>
        </div>
      </div>
      <div class="col-sm-8 col-md-9 col-lg-10">
        <table class=" user-list table table-striped">
        <thead>
          <tr>
            <td>
              <a href="#">
                Image 
              </a>
            </td>
            <td>
              <a href="#">
              Name 
              </a>
            </td>
            <td>
              <a href="#">
              Age 
              </a>
            </td>
            <td>
              <a href="#">
              Phone
              </a>
            </td>
          </tr>
        </thead>
        <tbody>
          <tr ng-cloak ng-click="showAnimal()" ng-repeat="animal in userList | orderBy:sortType:sortReverse |filter:searchAnimal">
            <td><img ng-src="{{ animal.image }}.svg" class="user-image"></td>
            <td>{{ animal.name }}</td>
            <td>{{ animal.age }}</td>
            <td>{{ animal.phone }}</td>
          </tr>
        </tbody>
      </table> 
      </div>
    </div>  
  </div>

这是有棱角的部分angular.module('displayApp',[])

.controller('mainController', function($scope, $http) {
  $scope.sortType     = 'name'; // set the default sort type
  $scope.sortReverse  = false;  // set the default sort order
  $scope.searchAnimal   = '';     // set the default search/filter term
  $http.get("data.json")
   .then(function(response) {
    $scope.userList = response.data;
    $scope.animName = $scope.userList[0].name;
    $scope.animImage = $scope.userList[0].image;
    $scope.animAge = $scope.userList[0].age;
    $scope.animPhone = $scope.userList[0].phone;
    $scope.animPhrase = $scope.userList[0].phrase;      
});
$scope.countryCode = "8";
$scope.showAnimal = function(){
 $scope.animName = this.animal.name;
 $scope.animImage = this.animal.image;
 $scope.animAge = this.animal.age;
 $scope.animPhone = this.animal.phone;
 $scope.animPhrase = this.animal.phrase;

}});

单击其中一个排序按钮时,需要调整clickAnimal()函数来更新$scope.showAnimal函数中的数据。这意味着当点击排序按钮时,将动物数据设置为表中的第一项。

有关如何更改$scope值的更多信息,请阅读以下内容:http://jimhoskins.com/2012/12/17/angularjs-and-apply.html

下面是关于如何使用$scope.apply的示例:http://jsfiddle.net/k19966h8/

一个片段:

function Ctrl($scope) {
  $scope.message = "Waiting 2000ms for update";
    setTimeout(function () {
        $scope.$apply(function () {
            $scope.message = "Timeout called!";
        });
    }, 2000);
}