小编典典
此解决方案不使用硬编码模板,您可以编译嵌入在API响应中的Angular表达式。
步骤1. 安装此指令:https :
//github.com/incuna/angular-bind-html-
compile
步骤2. 将指令包括在模块中。
angular.module("app", ["angular-bind-html-compile"])
步骤3. 在模板中使用指令:
结果:
控制器对象
$scope.letter = { user: { name: "John"}}
JSON回应
{ "letterTemplate":[
{ content: "Dear {{letter.user.name}}," }
]}
HTML输出=
Dear John,
为了参考起见,以下是相关指令:
(function () {
'use strict';
var module = angular.module('angular-bind-html-compile', []);
module.directive('bindHtmlCompile', ['$compile', function ($compile) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
scope.$watch(function () {
return scope.$eval(attrs.bindHtmlCompile);
}, function (value) {
element.html(value);
$compile(element.contents())(scope);
});
}
};
}]);
}());
2020-04-25