【发布时间】:2016-08-24 20:05:54
【问题描述】:
我对 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>`;
这是指令,最后一个指令“usersCustomDirective”是应该拉入其他指令的指令。
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 被渲染,自定义标签没有。控制台记录了传入的字符串值。我还尝试将“usersCustomDirective”更改为:
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 中显示任何内容,而是显示我正在传递的文本字符串。
【问题讨论】:
标签: javascript html angularjs typescript