使用IIFE实例化时,其他AngularJS控制器与CommonController [英] Instantiate other AngularJS-controller with a CommonController when using IIFE

查看:116
本文介绍了使用IIFE实例化时,其他AngularJS控制器与CommonController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中很多机型都将用几乎同样的控制器 - code。与它们调用不同的服务,唯一的例外管理。

I have a project where a lot of the models are going to be managed by almost the same controller-code with the only exception that they are calling different services.

我现在处理这个的方法是通过实例化一个CRUD - 控制器与普通code到每一个定制的控制器,然后通过改变自定义控制器内部变量重定向服务呼叫。即
vm.service.get()通过设置渣滓 - 控制器内的变化 vm.service = teamService; 在自定义控制器。

The way I'm handling this now is by instantiating a Crud-Controller with common code into every custom controller and then redirecting the service-call by changing the variable inside the custom controller. I.e. vm.service.get() inside the Crud-Controller changes by setting vm.service = teamService; in a custom controller.

这是我如何实例化渣滓 - 控制器到我的自定义控制器的时刻:

This is how I instantiate the Crud-Controller into my custom controllers at the moment:

$injector.invoke(Crud, this, {$scope:$scope});

这工作得很好,但我不知道这是否是分享共同的控制器code的正确途径。也许是可能的实例为此使用的服务?因为我有问题(如果我做的方式,它是正确的),我该如何访问其他控制器,同时使用IIFE?现在我不使用IIFE因为我还没有想出一个办法这样做。

This works fine, however I don't know if this is the right way to share common controller code. Maybe it is possible to instantiate a service for this use? Because the question I have (if my way of doing it is correct), how do I access other controllers while using IIFE? Right now I am not using IIFE since I have not figured out a way to do so.

我曾尝试与 angular.module(应用)。控制器('界面污物'),但它不工作。

I have tried with angular.module('app').controller('Crud') but it does not work.

即:如何获得访问PrimaryCtrl的编辑功能,而无需使用$注射器或依赖于$范围继承?
http://jsfiddle.net/tcVhN/62/

I.e: How do I get access to the PrimaryCtrl's edit function without using $injector or relying on the $scope inheritance? http://jsfiddle.net/tcVhN/62/

推荐答案

它看起来像你的榜样使用支持开箱即用全局控制器角度1.0.x的。 那怎么会做,提供更近角,没有去太深成JS继承的危险。

It looks like your example uses Angular 1.0.x that supports global controllers out of the box. That's how it would be done with more recent Angular, without going too deep into the perils of JS inheritance.

'use strict';

(function (root, angular) {
    root.ctrls = root.ctrls || {};

    var primaryCtrl = function ($scope) {
        var self = this;
        console.log('primaryCtrl constructor', self, $scope);
    };
    primaryCtrl.prototype = {
        items: ['Item 1', 'Item 2'],
        edit: function (item) {
            //do stuff
        }
    };
    primaryCtrl.$inject = ['$scope'];

    root.ctrls.primaryCtrl = primaryCtrl;
})(this, angular);

(function (root, angular) {
    root.ctrls = root.ctrls || {};

    var secondaryCtrl = function ($scope) {
        var self = this;
        console.log('secondaryCtrl constructor', self, $scope);
    };
    secondaryCtrl.prototype = angular.extend({},
        root.ctrls.primaryCtrl.prototype,
        {
            items: ['Stuff 1', 'Stuff 2']
        }
    );
    secondaryCtrl.$inject = ['$scope'];

    root.ctrls.secondaryCtrl = secondaryCtrl;
})(this, angular);

var app = angular.module('app',[]);
app.controller('PrimaryCtrl', ctrls.primaryCtrl);
app.controller('SecondaryCtrl', ctrls.secondaryCtrl);

<div ng-controller="PrimaryCtrl as primary">
    <p ng-repeat="item in primary.items">{{item}}</p>
</div>

<div ng-controller="SecondaryCtrl as secondary">
    <p ng-repeat="item in secondary.items">{{item}}</p>
</div>

您还可以检查角优等,带来自以为是,但可行的语法延伸到角。

You may also check Angular Classy, which brings opinionated but viable extending syntax to Angular.

这篇关于使用IIFE实例化时,其他AngularJS控制器与CommonController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆