我对angularjs比较新。我正在客户端使用angularjs创建一个Web应用程序。我已经制作了几个自定义指令,并希望客户能够自定义每次登录时看到的指令。我尝试做的是创建一个字符串,其中填充了用户选择的自定义指令标记。此信息将作为cookie值保存,并在用户登录时由控制器加载。
这是传递的cookie值(我现在硬编码):
public usersCustoms = `<div class="mdl-grid">
<div id="firstrow" class="mdl-cell mdl-cell--12-col mdl-cell--8-col-tablet mdl-grid" >
<cit-recap > </cit-recap>
<veh-ar-recap></veh-ar-recap>
<new-models-inv></new-models-inv>
<used-models-inv></used-models-inv>
</div>
<div id="secondrow" class="mdl-cell mdl-cell--12-col mdl-cell--8-col-tablet mdl-grid">
<deal-recap></deal-recap>
<vehicle-inv></vehicle-inv>
<acct-trend></acct-trend>
</div>
<div id="thirdrow" class="mdl-cell mdl-cell--12-col mdl-cell--8-col-tablet mdl-grid">
<parts-inv></parts-inv>
</div>
</div>`;
以下是最后一条指令的指令&#34; usersCustomDirective&#34;是应该拉其他指令的那个。
namespace DashboardDemo {
function citRecap(): ng.IDirective {
return {
templateUrl: 'ngDashboardDemo/Views/Directives/citrecap.html',
restrict: 'AE',
replace: true,
controller: DashboardDemo.DashboardController,
controllerAs: 'dc',
link: function (scope, elem, attrs) {
}
}
}
angular.module('DashboardDemo').directive('citRecap', citRecap);
function vehArRecap(): ng.IDirective {
return {
templateUrl: 'ngDashboardDemo/Views/Directives/vehArRecap.html',
restrict: 'AE',
replace: true,
controller: DashboardDemo.DashboardController,
controllerAs: 'dc',
link: function (scope, elem, attrs) {
}
}
}
angular.module('DashboardDemo').directive('vehArRecap', vehArRecap);
function newModelsInv(): ng.IDirective {
return {
templateUrl: 'ngDashboardDemo/Views/Directives/newModelsInv.html',
restrict: 'AE',
replace: true,
controller: DashboardDemo.DashboardController,
controllerAs: 'dc',
link: function (scope, elem, attrs) {
}
}
}
angular.module('DashboardDemo').directive('newModelsInv', newModelsInv);
function usedModelsInv(): ng.IDirective {
return {
templateUrl: 'ngDashboardDemo/Views/Directives/usedModelsInv.html',
restrict: 'AE',
replace: true,
controller: DashboardDemo.DashboardController,
controllerAs: 'dc',
link: function (scope, elem, attrs) {
}
}
}
angular.module('DashboardDemo').directive('usedModelsInv', usedModelsInv);
function dealRecap(): ng.IDirective {
return {
templateUrl: 'ngDashboardDemo/Views/Directives/dealRecap.html',
restrict: 'AE',
replace: true,
controller: DashboardDemo.DashboardController,
controllerAs: 'dc',
link: function (scope, elem, attrs) {
}
}
}
angular.module('DashboardDemo').directive('dealRecap', dealRecap);
function vehicleInv(): ng.IDirective {
return {
templateUrl: 'ngDashboardDemo/Views/Directives/vehicleInv.html',
restrict: 'AE',
replace: true,
controller: DashboardDemo.DashboardController,
controllerAs: 'dc',
link: function (scope, elem, attrs) {
}
}
}
angular.module('DashboardDemo').directive('vehicleInv', vehicleInv);
function acctTrend(): ng.IDirective {
return {
templateUrl: 'ngDashboardDemo/Views/Directives/acctngTrend.html',
restrict: 'AE',
replace: true,
controller: DashboardDemo.DashboardController,
controllerAs: 'dc',
link: function (scope, elem, attrs) {
}
}
}
angular.module('DashboardDemo').directive('acctTrend', acctTrend);
function partsInv(): ng.IDirective {
return {
templateUrl: 'ngDashboardDemo/Views/Directives/partsinv.html',
restrict: 'AE',
replace: true,
controller: DashboardDemo.DashboardController,
controllerAs: 'dc',
link: function (scope, elem, attrs) {
}
}
}
angular.module('DashboardDemo').directive('partsInv', partsInv);
function usersCustomDirective(): ng.IDirective {
return {
link: function (scope, elem, attrs) {
//elem.html(scope.dc.usersCustoms);
//console.log(scope.dc.usersCustoms);
elem.html(scope.userOptions);
},
restrict: 'AEC',
replace: true,
transclude: true,
controller: DashboardController,
controllerAs: 'dc',
scope: {
userOptions: '@userOptions'
},
//template: scope.userOptions
}
}
angular.module('DashboardDemo').directive('usersCustomDirective', usersCustomDirective);
}
以下是该视图的HTML:
<main class="mdl-color--grey-100">
<users-custom-directive user-options ="{{dc.usersCustoms}}"></users-custom-directive></main>
正如现在所写,html没有显示我假设因为html不能识别我的自定义标签。我也尝试在链接功能中使用elem.replaceAll但得到了类似的结果。当我在chrome中打开开发人员工具时,我在div中看到我的div和我的自定义标签,但是,只有div被渲染,自定义标签不是。控制台记录了传入的字符串值。我还尝试更改&#34; usersCustomDirective&#34;对此:
function usersCustomDirective(): ng.IDirective {
return {
link: function (scope, elem, attrs) {
//elem.html(scope.dc.usersCustoms);
//console.log(scope.dc.usersCustoms);
console.log(scope.userOptions);
},
restrict: 'AEC',
replace: true,
transclude: true,
controller: DashboardController,
controllerAs: 'dc',
scope: {
userOptions: '@userOptions'
},
template: scope.userOptions
}
}
它不会在html中显示任何内容,而是显示我经过的文本字符串。
答案 0 :(得分:0)
好的,我想出了我需要做的事情。经过进一步研究,我需要在此过程中包含$ compile。我所做的更改是将$ compile添加到指令函数调用中。然后我为模板分配了scope.userOptions值。我在模板上调用了$ compile和viola angularjs magic。现在很棒。希望这有助于其他人。仅供参考,如果您正在使用打字稿,就像我一样,您会收到错误消息:“IScope类型中不存在属性空白”,您需要创建自己的接口并扩展IScope以包含属性
function usersCustomDirective($compile): ng.IDirective {
return {
link: function (scope: IMyScope, elem, attrs) {
var template = scope.userOptions;
var linkFn = $compile(template);
var content = linkFn(scope);
elem.append(content);
},
restrict: 'AEC',
replace: true,
controller: DashboardController,
controllerAs: 'dc',
scope: {
userOptions: '@userOptions'
},
}
}
angular.module('DashboardDemo').directive('usersCustomDirective', usersCustomDirective);