如何使用ng-包括代码?

时间:2022-08-25 23:58:27

I want to use ng-include directive from the code. Something like

我想使用ng-include指令从代码。类似的

var html = '<div ng-include="\'myTemplate.html\'"></div>';
$compile(html)($scope);
angular.element(document.getElementById('myDiv')).append(html);

but, this one is not working as expected. Can anyone explain how to achieve this properly?

但是,这一项并没有达到预期的效果。有人能解释如何正确地做到这一点吗?

2 个解决方案

#1


0  

If you want include html into your webpage, you should use ng-bing-html instead :

如果你想把html包含到你的网页中,你应该使用ng- bhtml代替:

HTML

HTML

<div id="myDiv" ng-bind-html="template"></div>

JS

JS

.controller('name', function($scope, $http, $sce, ...) {
    $http.get('myTemplate.html').success(function(template) {
        $scope.template = $sce.trustAsHtml(template);
    });
});

#2


0  

Change the order of your statements.

更改语句的顺序。

var html = '<div id="dynamicID" ng-include="\'myTemplate.html\'"></div>';
var ngEl = angular.element(document.getElementById('myDiv'));
ngEl.append(html);
$compile(ngEl.find('#dynamicID'))($scope);

#1


0  

If you want include html into your webpage, you should use ng-bing-html instead :

如果你想把html包含到你的网页中,你应该使用ng- bhtml代替:

HTML

HTML

<div id="myDiv" ng-bind-html="template"></div>

JS

JS

.controller('name', function($scope, $http, $sce, ...) {
    $http.get('myTemplate.html').success(function(template) {
        $scope.template = $sce.trustAsHtml(template);
    });
});

#2


0  

Change the order of your statements.

更改语句的顺序。

var html = '<div id="dynamicID" ng-include="\'myTemplate.html\'"></div>';
var ngEl = angular.element(document.getElementById('myDiv'));
ngEl.append(html);
$compile(ngEl.find('#dynamicID'))($scope);