简体   繁体   English

AngularJS搜索框指令或如何避免控制器中的代码重复

[英]AngularJS searchbox directive or how to avoid code duplication in controller

I'm working on multiple school student management application. 我正在研究多个学校的学生管理应用程序。 The are multiple roles: school manager and super manager. 有多个角色:学校经理和超级经理。 The school manager can only search students in his school while super manager can search for student in all schools. 学校管理员只能在其学校中搜索学生,而超级管理员可以在所有学校中搜索学生。

Because the search is done only by removing the school id in second case, I think that the same searchbox can be used, and the same functions for searching. 因为在第二种情况下,仅通过删除学校ID来完成搜索,所以我认为可以使用相同的搜索框和相同的搜索功能。

This is the template: 这是模板:

<div class="input-group">
    <div class="input-group-btn">
        <button type="button" class="btn btn-default" tabindex="-1">{{ searchBy | translate }}</button>
        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" tabindex="-1">
            <span class="caret"></span>
            <span class="sr-only">Toggle Dropdown</span>
        </button>
        <ul class="dropdown-menu" role="menu">
            <li ng-repeat="value in searchByFields"><a ng-click="searchByAttr(value)">{{ value | translate }}</a></li>
        </ul>
    </div>
    <form ng-submit="search(searchValue)">
        <input type="text" 
            ng-model="searchValue"  
            class="form-control" 
            placeholder="Search.." />
    </form>
    <span class="input-group-btn">
        <button class="btn btn-warning" type="button">
            <i class="fa fa-search fa-fw"></i>
        </button>
    </span>
</div>

This is the controller scope functions and variables, that is used in search: 这是在搜索中使用的控制器作用域函数和变量:

$scope.searchBy = 'cn';
$scope.searchByFields = ['cn', 'ou', 'o'];

$scope.searchByAttr = function (value) { 
    $scope.searchBy = value;
}

var searchFn = function (params) {
    User.search(params).success(function (data) {
        $scope.students = data.results;
        $scope.collapsedTableRows = [];
        _.each($scope.students, function () {
            $scope.collapsedTableRows.push(true);
        });
    }).error(function (reason) {
        $scope.students = [];
        $scope.collapsedTableRows = [];
        var log = Logger.getInstance("AdminController(searchFn)");
        var error = reason[0];
        log.error('name - \"{0}\", location - \"{1}\", error - \"{2}\"',
            [error.name, error.location, error.description]);
    });
}

$scope.search = function (value) {
    if(value) {
        var params = {
            search: [
                {attr: 'orgId', value: $injector.get('ORG_DN').replace('%id%', $scope.schoolId) },
                {attr: $scope.searchBy, value: '*' + value + '*' }
            ]
        };
        searchFn(params);
    }
}

As you can see, my search results are stored in scope variables, which are used in table directive. 如您所见,我的搜索结果存储在作用域变量中,该变量在表指令中使用。 I have the same code in two controllers, which is annoying. 我在两个控制器中有相同的代码,这很烦人。 How should I construct directive from what I have for search, so that I can pass scope variables to directive and get data from it for my other directive to work correctly? 我应该如何从搜索内容中构造指令,以便可以将范围变量传递给指令并从中获取数据以使我的其他指令正常工作?

Create a angular service: Source Tutorial 创建角度服务: 源教程

Your factory can contain data manipulated in a single place, but it also has common operations. 您的工厂可以包含在单个位置处理的数据,但是它也具有通用的操作。 It's passable to a controller while creating the controller. 在创建控制器时,它可以传递给控制器​​。

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

app.service('MathService', function() {
    this.add = function(a, b) { return a + b };
    this.subtract = function(a, b) { return a - b };
    this.multiply = function(a, b) { return a * b };
    this.divide = function(a, b) { return a / b };
});

app.service('CalculatorService', function(MathService){
    this.square = function(a) { return MathService.multiply(a,a); };
    this.cube = function(a) { return MathService.multiply(a, MathService.multiply(a,a)); };
});

app.controller('CalculatorController', function($scope, CalculatorService) {
    $scope.doSquare = function() {
        $scope.answer = CalculatorService.square($scope.number);
    }
    $scope.doCube = function() {
        $scope.answer = CalculatorService.cube($scope.number);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM