gpt4 book ai didi

javascript - 从 AngularJS 模板字符串生成 HTML 字符串

转载 作者:可可西里 更新时间:2023-11-01 02:22:48 30 4
gpt4 key购买 nike

我将 angularjs 模板作为一个字符串,包括“ng-repeat”和其他指令。我想在 Controller 中编译它以将结果 HTML 生成为字符串。

我想在 Angular 中应用的示例:

Input:
-------
var template = '<div ng-repeat="item in items">{{item.data}}</div>';

Output:
--------
var result = '<div>1</div><div>2</div><div>3</div><div>4</div>';

我希望在我拥有的 Controller 中完成此操作,并且我尝试了以下操作:

var template = '<div ng-repeat="item in items">{{item.data}}</div>';
var linkFunction = $compile(template);
var result = linkFunction($scope);

console.log(result); // prints the template itself!

谢谢!

最佳答案

试一试:

var template = angular.element('<div ng-repeat="item in items">{{item.data}}</div>');
var linkFunction = $compile(template);
var result = linkFunction($scope);
$scope.$apply();

console.log(template.html());

关于javascript - 从 AngularJS 模板字符串生成 HTML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26386470/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com