繁体   English   中英

如何使用$ compile服务通过AngularJS指令包含HTML数据

[英]How to use the $compile service to include HTML data with AngularJS directives

我有一个使用ng-repeat将内容填充到内容区域的控制器。 问题在于,其中一些内容需要放在模板文件的前面,因此需要“即时”编译。 现在,我有此功能可以动态添加内容:

$scope.layouts = [

    { id: 'Dashboard', icon: 'dashboard', view: '/qph/views/Dashboard.php' },
    { id: 'Customers', icon: 'people', view: '/qph/views/users.php' },
    { id: 'Quotes', icon: 'format_list_bulleted', view: '/qph/views/Quotes.php' }

];

$scope.workspace = {};    

var getTemplate = function(id){

    var view = 'test.php';

    $timeout(function() { //added timeout

    if($templateCache.get(view) === undefined) {
            $templateRequest(view).then(function (data) {
                $scope.workspaces.forEach(function (v) {
                    if (v.id == id) v.content = $compile(data)($scope);
                });
            });

    } else {
        $scope.workspaces.forEach(function (v) {
            if (v.id == id) v.content = $compile($templateCache.get(view))($scope);
        });

    }
    }, 2000);
};

$scope.workspaces =
    [
        { id: 1, name: "Dashboard", icon: 'dashboard', active:true  }
    ];
getTemplate(1);

我检查了数据变量是否具有预期的html内容,但是编译输出如下:

{"0":{"jQuery331075208394539601512":{"$scope":"$SCOPE","$ngControllerController":{}}},"length":1}

有谁知道为什么它没有按预期方式编译html内容?

以下是模板内容供参考:

<div class="col-sm-6 col-sm-offset-3" ng-controller="UserController">
<div class="col-sm-6 col-sm-offset-3">
    <div class="well">
        <h3>Users</h3>
        <button class="btn btn-primary" style="margin-bottom: 10px" ng-click="user.getUsers()">Get Users!</button>
        <ul class="list-group" ng-if="user.users">
            <li class="list-group-item" ng-repeat="user in user.users">
                <h4>{{user.name}}</h4>
                <h5>{{user.email}}</h5>
            </li>
        </ul>
        <div class="alert alert-danger" ng-if="user.error">
            <strong>There was an error: </strong> {{user.error.error}}
            <br>Please go back and login again
        </div>
    </div>
</div>
</div>

这是用于显示已编译内容的选项卡视图:

<ul class="nav nav-tabs workspace-tabs">
    <li class="nav-item" ng-repeat="space in workspaces">
        <a class="nav-link" data-toggle="tab" href="#workspace{{space.id}}" ng-class="(space.active == true ) ? 'active show': ''">
            <span class="hidden-sm-up"><i class="material-icons md-24">{{space.icon}}</i></span>
            <span class="hidden-xs-down">{{space.name}}</span>
            <button ng-click="workspace.remove($index)">x</button>
        </a>
    </li>
</ul>

<div class="tab-content workspace-content">
    <div ng-repeat="space in workspaces" id="workspace{{space.id}}" class="tab-pane fade in" ng-class="(space.active == true ) ? 'active show': ''">
        {{space.content}}
    </div>
</div>

使用指令。

app.directive('myCustomer', function() {
  return {
    templateUrl: 'test.php',
    controller: 'UserController'
  };
})

模板缓存将被自动管理。

$ compile服务创建一个jqLit​​e对象,该对象需要使用jqLit​​e或jQuery append()方法添加到DOM中。 使用插值{{ }}将仅呈现jqLite对象的字符串化值。

<div class="tab-content workspace-content">
    <div ng-repeat="space in workspaces" id="workspace{{space.id}}" class="tab-pane fade in" ng-class="(space.active == true ) ? 'active show': ''">
        ̶{̶{̶s̶p̶a̶c̶e̶.̶c̶o̶n̶t̶e̶n̶t̶}̶}̶
        <compile html="space.html"></compile>
    </div>
</div>

而是使用自定义指令来编译HTML数据并将其附加到DOM:

app.directive("compile", function($compile) {
    return {
        link: postLink,
    };
    function postLink(scope, elem, attrs) {
        var rawHTML = scope.$eval(attrs.html)
        var linkFn = $compile(rawHTML);
        var $html = linkFn(scope);
        elem.append($html);
    }
})

有关更多信息,请参见《 AngularJS开发人员指南-HTML编译器》

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM