AngularJS - 不使用角度模板动态添加指令

时间:2015-06-03 12:57:49

标签: javascript angularjs angularjs-directive

我要求所有 html代码来自服务器。而html代码看起来像这样

for (task in subTasks) {

    var subTaskObj = subTasks[task];
    var taskTitle = subTaskObj.title;
    var taskStatus = subTaskObj.status;

    taskDOM += '<div class="oaerror danger" long-press>' +
        '<div class="col-xs-11"><strong>' + taskTitle + '</strong></div>' +
        '<div class="col-xs-1 pull-right"><img src="images/volunteer-task.png"></div>' +
        '<div class="clearfix"></div>' +
        '</div>';
}

taskDescriptionDOM = '<div class="error-notice" id=' + '"' + taskId + '"' + '>' +
    '<div class="oaerror danger">' +
    '<div class="col-xs-11"><strong>' + description + '</strong></div>' +
    '<div class="col-xs-1 pull-right"><img src="images/volunteer-task.png"></div>' +
    '<div class="clearfix"></div>' +
    '</div>' +
    '</div>' + taskDOM;
$("#widRes_01").html(taskDescriptionDOM);

如您所见,我还在taskDOM中添加了长按指令 所以我的问题是,为什么在将动态添加为

之前,该指令仍然无法编译

tgDynamic Directive

angular.module('Modern.directives')
    .directive('tgDynamic', function($compile, WidgetService) {
    return {
        restrict: 'E',
        scope: {
            groupWidget: '@'
        },
        template: '<div/>',
        replace: true,
        link: function(scope, ele, attr) {

            scope.$watch('groupWidget', function(newVal) {
                if (newVal) {
                    widgetService.getWidgetTemplate({"groupWidgetId": newVal }) 
                    .then(function(widgetResponse) {
                        var template = widgetResponse.data.result.template;
                        $(ele).html($compile(template)(scope));
                    });
                }
            });
        }
    };
});

这就是我使用tgDynamic Directive的方式

 <tg-dynamic class="panel" id="widRes_01" group-widget={{displayedWidgetId}}></tg-dynamic>

长按指令

directive("longPress", function($compile, WidgetService) {
        return {
            scope: {},
            link: function(scope, ele, attr) {
                    var groupWidgetId = '',
                        isOpen = false;


                    $(ele).on('touchstart', function() {
                        var that = this,
                            dropDown = '<div id="widgetMenu"/>';

                        console.log("Touch Start");

                        setTimeout(function() {
                            groupWidgetId = $(ele).find('label#gmw_grpWidgetId').html();
                            var widgetType = $(ele).find('label#gmw_widgetType').html();
                            isOpen = widgetType === "OPEN" ? true : false;
                            if (isOpen) {
                                $(that).append(dropDown);
                                $("#widgetMenu").html($compile('<ul><li><a ng-click="updateIsMainStatus()">Mark as Main</a></li><li><a ng-click="closeWidget()">Close Widget</a></li></ul>')(scope));
                            }else
                                ShowDialogBox("INFO","Widget is closed.");
                        }, 1000);

                    });

                    $(ele).on('touchend', function() {
                        setTimeout(function() {
                            $("#widgetMenu").remove();
                        }, 2000);
                        console.log("Touch End");
                    });

1 个答案:

答案 0 :(得分:0)

template: '<div/>',

在指令中,template参数用于设置模板的呈现位置。在您的代码中,"<div/>"不是模板文件的位置。您应该遵循角度转换将html代码拆分为文件并将模板值设置为文件的位置