简体   繁体   English

角度指令 - 动态控制器

[英]Angular Directive - Dynamic Controller

I have been assigned the task of creating a set of "cards" like on Google one and other similar sites using AngularJS for a dashboard. 我被分配了创建一组“卡片”的任务,例如在Google和其他类似网站上使用AngularJS作为仪表板。 Each card has independent functionality from the rest and they all must appear as soon as their data has been returned from the API. 每张卡都具有独立的功能,它们都必须在从API返回数据后立即显示。 In the future we are going to allow for the pinning/unpinning of these cards to the dashboard, as well as ordering to be customized by the user. 将来我们将允许将这些卡固定/取消固定到仪表板,以及由用户定制的订购。

I have created a "card-dealer" directive that will contain an ng-repeat for each card in an array of card objects. 我创建了一个“卡片经销商”指令,它将在卡片对象数组中为每张卡片包含一个ng-repeat。 Each of these cards needs to have their own dynamic template and controller, which is where I have ran into an issue. 这些卡中的每一个都需要有自己的动态模板和控制器,这是我遇到问题的地方。 I was able to figure out how to pass in a dynamic templateUrl , but the controller is a different story. 我能够弄清楚如何传递动态templateUrl ,但控制器是一个不同的故事。 I have all but got it figured out by mimicking what I watched on a Pluralsight video, I should just be able to pass a card.controller variable into the directive "ctrl" attribute like this: 我只是通过模仿我在Pluralsight视频上看到的内容得到它,我应该能够将card.controller变量传递到指令“ctrl”属性,如下所示:

<card-dealer ng-repeat="card in cards" card="card" ctrl="{{card.controller}}"/>

However, the ctrl='{{card.controller}}' variable in the directive is not being parsed before it is sent to the directive. 但是,指令中的ctrl='{{card.controller}}'变量在发送到指令之前不会被解析。 In the directive I set the controller dynamically by setting controller: '@' and name: 'ctrl' . 在指令中,我通过设置控制器动态设置controller: '@'name: 'ctrl' This looks into the element for the attribute named "ctrl" and returns it's value as the name of the controller. 这将查看名为“ctrl”的属性的元素,并将其值作为控制器的名称返回。 This method works when I type in the string name of the directive controller instead of using the {{card.controller}} variable, but simply typing a string isn't dynamic. 当我输入指令控制器的字符串名称而不是使用{{card.controller}}变量时,此方法有效,但只是键入字符串不是动态的。

I need the variable to be parsed before it reaches the directive so that it looks for the proper controller name. 我需要在变量到达指令之前进行解析,以便查找正确的控制器名称。 I have created a JS Fiddle to demonstrate my issue: http://jsfiddle.net/kPdCk/ . 我创建了一个JS小提琴来演示我的问题: http//jsfiddle.net/kPdCk/ This should return two alert boxes, one representing each directive controller. 这应该返回两个警告框,一个代表每个指令控制器。 If you run this currently, you'll see in the console log that it is trying to search for a controller with the name of the variable instead of the value of the variable. 如果您当前运行此命令,您将在控制台日志中看到它正在尝试搜索具有变量名称而不是变量值的控制器。

Please help!! 请帮忙!! This is crucial to the future of our entire dashboard, once we figure out how to pass in a dynamic controller name we will be golden. 这对我们整个仪表板的未来至关重要,一旦我们弄清楚如何传递动态控制器名称,我们将是金色的。 Thanks in advance!!! 提前致谢!!!

This is how it is done: 这是如何做到的:


Inside your directive all you need is an attribute which gives you access to the name of the card: <card-dealer ng-repeat="card in cards" card="card"> </card-dealer> in my case my card attribute holds a card object which has a name property. 在您的指令中,您只需要一个属性,您可以访问该卡的名称: <card-dealer ng-repeat="card in cards" card="card"> </card-dealer>在我的情况下我的卡attribute包含一个具有name属性的card对象。 In the directive you set the isolate scope to: scope: { card: '=' } This isolates and interpolates the card object to the directive scope. 在指令中,您将隔离范围设置为: scope: { card: '=' }这会将卡对象隔离并插入指令范围。 You then set the directive template to: template: '<div ng-include="getTemplateUrl()"></div>' , this looks to the directive's controller for a function named getTemplateUrl. 然后,将指令模板设置为: template: '<div ng-include="getTemplateUrl()"></div>' ,这将向指令的控制器查找名为getTemplateUrl的函数。 This is what you want because the directive controller has access to the scope object. 这是您想要的,因为指令控制器可以访问范围对象。 In the directive controller the getTemplateUrl function looks like this: controller: ['$scope', '$attrs', function ($scope, $attrs) { $scope.getTemplateUrl = function () { return '/View/Card?cardName=' + $scope.card.name; }; }], 在指令控制器中,getTemplateUrl函数如下所示: controller: ['$scope', '$attrs', function ($scope, $attrs) { $scope.getTemplateUrl = function () { return '/View/Card?cardName=' + $scope.card.name; }; }], controller: ['$scope', '$attrs', function ($scope, $attrs) { $scope.getTemplateUrl = function () { return '/View/Card?cardName=' + $scope.card.name; }; }],


I have an mvc controller which links up the proper .cshtml file and handles security when this route is hit, but this would work with a regular angular route as well. 我有一个mvc控制器,它链接正确的.cshtml文件,并在命中此路由时处理安全性,但这也适用于常规的角度路由。 In the .cshtml file you set up your dynamic controller by simply putting <div ng-controller="CardContactController"></div> as the root element. 在.cshtml文件中,您只需将<div ng-controller="CardContactController"></div>作为根元素即可设置动态控制器。 The controller will differ for each card. 每张卡的控制器都不同。 This creates a hierarchy of controllers which allows you to apply additional logic to all cards in general, and then specific logic to each individual card. 这将创建一个控制器层次结构,允许您将一般逻辑应用于所有卡,然后为每个卡应用特定逻辑。 I still have to figure out how I'm going to handle my services but this approach allows you to create a dynamic templateUrl and dynamic controller for a directive using an ng-repeat based on the card name alone. 我仍然需要弄清楚我将如何处理我的服务,但这种方法允许您使用基于卡名称的ng-repeat为指令创建动态templateUrl和动态控制器。 It is a very clean way of accomplishing this functionality and it is all self-contained. 这是完成此功能的一种非常干净的方式,它是完全独立的。

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

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