简体   繁体   English

如何在同一页上多次使用角度控制器?

[英]How To Use An Angular Controller Multiple Times On The Same Page?

I am building a simple dashboard with 6 slots. 我正在构建一个带有6个插槽的简单仪表板。 Users can select the type of content they'd like to put in each slot, and they can also select the same content to be put into multiple slots. 用户可以选择他们想要放入每个插槽的内容类型,他们也可以选择要放入多个插槽的相同内容。 This is where the problem lies... 这就是问题所在......

I'd like to architect the app so that the content template is injected dynamically into slots via ng-include , and the content template will contain a ng-controller="Content1Controller" reference to the controller for that content. 我想构建应用程序,以便通过ng-include动态地将内容模板注入到插槽中,并且内容模板将包含对该内容ng-controller="Content1Controller"ng-controller="Content1Controller"引用。

But, when adding the same content in multiple slots, the controller is re-run each time for multiple instances of content. 但是,在多个插槽中添加相同内容时,每次都会为多个内容实例重新运行控制器。

Since keeping track of the instances within the controller won't work since it potentially can be re-run, should I track instances in a Service, and have the controller check in with the Service each time it is run? 由于跟踪控制器中的实例是不可行的,因为它可能可以重新运行,我应该跟踪服务中的实例,并让控制器每次运行时都检查服务吗?

My question is how should I architect this, and am I thinking about this in a correct/angular-ish way? 我的问题是我应该如何构建这个,我是否以正确/有角度的方式思考这个问题?

Services are great ways to store common data in an Angular app, so that would be my first recommendation about how to architect your app. 服务是在Angular应用程序中存储常见数据的好方法,因此这是我关于如何构建应用程序的第一个建议。

Digging deeper, I think your decision really depends on what you're using your data for. 深入挖掘,我认为您的决定实际上取决于您使用数据的原因。 Re-using the same service is probably fine if each slot is self-contained, and won't need to really interact much with other slots. 如果每个插槽都是自包含的,则重新使用相同的服务可能很好,并且不需要与其他插槽真正交互太多。 If this is the case, then you don't even need services: each controller can simply keep its own local data copy, and everything will be fine. 如果是这种情况,那么你甚至不需要服务:每个控制器都可以简单地保留自己的本地数据副本,一切都会好的。

For example, 例如,

angular.module('mean.root').controller('ContentA1Controller', function($scope){
    //each instance's scope will keep its own local data,
    //but other scopes won't really have access to it
    $scope.data = [1,2,3,4,5]
});

If, however, you need to share each controller's data with other elements of the page, a service can help you there. 但是,如果您需要与页面的其他元素共享每个控制器的数据,那么服务可以帮助您。

//factory also works instead of service; use whichever you prefer
angular.module('mean.root').service('MyDataService',function(){
    this.bucket = {};

    this.saveData = function(data,id){
        this.bucket[id]=data;
    }

    this.getData = function(id){
        return this.bucket[id];
    }
});

Then your controller is declared like 然后你的控制器被声明为

<div ng-controller='MyController' number='2'>

(or, instead of 'number', any custom attribute that indentifies which instance it is) And your controller code is (或者,而不是“数字”,任何标识它是什么实例的自定义属性)和你的控制器代码是

angular.module('mean.root').controller('ContentA1Controller', function($scope, $attrs, MyDataService){
    //I'm skipping data setup, null checking, etc., so be sure to add those!
    $scope.data = MyDataService.getData($attrs.number);

    //do some cool stuff here!

    //Make a 'save' function to put the data back
    $scope.save = function(){
       MyDataService.saveData($scope.data, $attrs.number);
    }
});

By using a service, other controllers (and even services) can access the data each slot is using, without having to dig down and find the exact $scope of that controller. 通过使用服务,其他控制器(甚至服务)可以访问每个插槽正在使用的数据,而无需挖掘并找到该控制器的确切$范围。

Also, depending on how your app is set up, you may need your view to update when your service's data changes; 此外,根据您的应用的设置方式,您可能需要在服务数据发生变化时更新您的视图; that's very easy to do using $scope.$watch() . 使用$scope.$watch()非常容易$scope.$watch() You can read this SO post to find out more about how to do that. 您可以阅读此SO帖子以了解有关如何执行此操作的更多信息。

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

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