【问题标题】:Using angular directives with prototype or object initializar approach使用带有原型或对象初始化方法的角度指令
【发布时间】:2015-09-02 03:59:50
【问题描述】:

我正在尝试将角度指令与原型或对象初始化程序一起使用。例子:

原型:

'use strict';
var Example = function Example($scope, $elem) {
  this.$scope = $scope;
  this.$elem  = $elem;

  this.init();
};

Example.prototype.init = function() {
  /// rest
};

var exampleDirective = function() {
    return {
        restrict: 'M',
        link: function($scope, $elem) {
            return new Example($scope, $elem);
        }
    };
};

angular
    .module('adminApp')
    .directive('exampleDirective', exampleDirective);

对象初始化器:

'use strict';
var Example = {
  init: function($scope, $elem) {
    this.$scope = $scope;
    this.$elem  = $elem;


    // rest
  }
};

var exampleDirective = function() {
    return {
        restrict: 'M',
        link: function($scope, $elem) {
            return Example.init($scope, $elem);
        }
    };
};

angular
    .module('adminApp')
    .directive('exampleDirective', exampleDirective);

两者都有效,但我想弄清楚我应该如何使用,如果有约定的话。

我已经搜索了很多关于使用这样的角度指令,我找到的最接近的答案是:Let's Make Full-Ass Angular Directives

我正在寻找一种不同的方法。

谢谢。

【问题讨论】:

标签: javascript angularjs oop


【解决方案1】:

这是我在整个应用程序中为分页所做的服务...我认为它可以通过提供一种在指令中使用构造函数的方式来帮助您...这不是一个完整的答案,但也许它会有所帮助。

'use strict';

/**
 * Paginator Service
 */

angular.module('paginatorService', [])
        .factory("paginatorService", function () {

            var ConstructorPaginator = function (scope, changeSearchParameter, type) {
                this._fillPaginatorScope(scope);
                this._setUpWatchOnPageChanged(scope, changeSearchParameter);
            };

            ConstructorPaginator.prototype._fillPaginatorScope = function (scope) {
                this._scope = scope;
                this._scope.maxSize = 4;
                this._scope.currentPage = 1;
                this._scope.currentPage = 1;
                this._scope.page = 1;
                this._scope.rowsPerPageOptions = [
                    {amount: 10, label: '10 rows per page'},
                    {amount: 25, label: '25 rows per page'},
                    {amount: 50, label: '50 rows per page'},
                    {amount: 75, label: '75 rows per page'}
                ];

                this._scope.rowsPerPage = this._scope.rowsPerPageOptions[1];

            };

            ConstructorPaginator.prototype._setUpWatchOnPageChanged = function (scope, changeSearchParameter) {
                scope.$watch('rowsPerPage', function (newValue, oldValue) {
                    scope.pageChanged(changeSearchParameter);
                });
            };

            return {
                ConstructorPaginator: ConstructorPaginator
            };

        });

在这种情况下,我只是从控制器调用构造函数

angular.module('')
        .controller('Ctrl', ['$scope', 'paginatorService', function ($scope, paginatorService) {

                new paginatorService.ConstructorPaginator($scope, account_id, false);

【讨论】:

  • 那么,你不认为在原型中使用 Angular 是错误的吗?
  • 也许我是出于无知,但原型应该总是让访问速度更快......有时并不重要,但我看到的唯一不好的事情是必须在这里和那里输入“原型”。 ..我确实听到人们这么说
  • 我同意,我认为奇怪的角度自然地做到了。
猜你喜欢
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 2013-06-27
  • 2018-07-05
相关资源
最近更新 更多