简单的动态指令(带有动态控制器和作用域)
Simple dynamic directives (with dynamic controllers and scope)?
我觉得应该是一个相当简单的问题。我正在一个TypeScript + Angular的应用中工作。
在控制器中,我有一个我想使用的类似directives
的数组。这些是我在整个应用程序中使用的panels
。
我将给它们各种属性,如controller
, templateUrl
, data
等。它们在我的控制器中看起来是这样的:
public panels: IPanelConfig[] = [
{
controllerName: "MenuController",
templateUrl: "menu/menu.html",
data: // an object with some boring stuff in it
},
{
controllerName: "AnotherController",
templateUrl: "another/this/is/arbitrary.html",
data: {}
}
];
在我的view
中,我循环遍历每个panel
,并使用称为panel
的通用directive
来处理其余的工作。像这样:
<div ng-repeat="panel in vm.panels">
<div panel
paneldata="panel.data"
templateurl="panel.templateUrl"
ctrl="panel.controllerName"></div>
</div>
我的自定义panel
指令看起来像这样:
module App {
"use strict";
export class Panel {
public link:(scope:angular.IScope, element:angular.IAugmentedJQuery, attrs:angular.IAttributes) => void;
public restrict:string = "EA";
public controller:string = "@";
public name: string = 'ctrl';
public replace:boolean = true;
public scope:any = {"paneldata": "="};
constructor() {
console.log("panel directive constructor");
}
public compile(el, attrs){
el.append("<div ng-include=''"components/panels/" + attrs.templateurl + "'"'>");
}
}
// add the directive to the angular module
angular.module("app").directive("panel", [() => new App.Panel()]);
}
最后,我设置了一个动态控制器。在本例中,它是MenuController
。它很无聊,看起来像这样:
module App {
"use strict";
export class MenuController {
public scope: any = null;
public items: IMyItems[] = null;
public panelData: any = null;
public dataService: IDataService = null;
static $inject = ["$scope", "DataService"];
constructor($scope: any, DataService: IDataService) {
$scope.vm = this;
this.scope = $scope;
this.dataService = DataService;
$scope.$watch("paneldata", (v) => { this.panelData = v; });
this.init();
}
public init() {
this.dataService.someRequestToGetItems().then((results) => {
this.items = results; // <--- the problem!!! :(
});
}
}
// add the controller to the module
angular.module("app").controller("MenuController", App.MenuController);
}
在这一点上,所有都像我期望的那样工作。panelData
被填充,适当的view
和controller
被使用,似乎我是如此接近,但它还没有完全在那里。问题:问题是当我尝试在MenuController
的items
数组上使用ng-repeat
时。视图的行为就好像items
是空的(它不是——一个简单的console.log()
证明它包含了我所期望的)。
: 我最初建立了一个具有相同功能的Menu
指令。view
,获取items
的请求和循环能力,接受panelData
属性等都工作得很好。直到我试图让它更有活力时,我才遇到了麻烦。
我真正想要的:对我来说,当每个面板都有自己的指令时,有这个额外的panel
指令来漏斗似乎很愚蠢(这是我开始的方式)。我真正想要做的是:
<div ng-repeat="panel in vm.panels">
<div {{ panel.directiveName }} paneldata="panel.data"></div>
</div>
但这行不通。
我希望有任何建议或指导!
我有一种预感,这个问题可能与scope
有关,但我添加了:
public test: string = "A test";
到我的MenuController
,并成功地显示在我的视图
嗯…这并没有完全解决"为什么这个不工作"的问题,我最初问(我觉得愚蠢的不尝试这个前一段时间),但另一种方法来解决我的问题(和处理动态指令/控制器),是添加:
el.append("<div " + attrs.directivename + " paneldata='" + attrs.paneldata + "'></div>");
到我的panel
指令的compile()
函数,而不是用ng-include
做模板。我仍然需要一个指令"漏斗",但这将解决我的问题,现在(并希望其他人谁偶然发现这个!)。
- 动态加载angularjs并生成控制器和范围
- 如何从相应的控制器动态更新标题和描述
- 动态启用/禁用来自控制器的输入文本
- 在动态加载的html中使用角度控制器
- 将从控制器返回的selectbox值动态呈现为json
- Angular,将文本框的动态值传给控制器
- 如何在打开模型时动态加载控制器文件
- 将变量动态添加到每个控制器的作用域
- 设置控制器动态角度
- AngularJs:$routeProvider与动态控制器名称赢得't负载
- 带有 ECMA6 的指令的动态控制器
- 带有动态控制器和模板的AngularJs指令
- 具有“控制器作为”语法的动态控制器
- Angular JS ui-router - 加载动态视图和动态控制器
- 在ng重复中分配动态控制器
- 从作用域绑定的动态控制器
- 角度指令>动态控制器名称>插入控制器名称
- 将动态控制器传递给angular指令
- 如何在HttpPost中访问可能的动态控制器列表?
- 简单的动态指令(带有动态控制器和作用域)