angular directive doesnt render the template, after adding html dynamically in controller

独自空忆成欢 提交于 2019-12-11 07:23:45

问题


My problem is that my directive doesnt show the template. I added a html tag with specific name in my controller to the dom via "$sce.trustAsHtml(taskDirective)" and "$compile(taskDirective)($scope)". The controller-function inside the directive is called. But the template is not showing up.

Im using the $stateProvider, which calls the "TaskDetailCtrl" with the certain HTML.

Anybody can help?

Thank you!+

Controller:

app.controller("TaskDetailCtrl", function ($scope, $state, $stateParams, $sce, $compile) {

    cur_task = $stateParams.newTask;
    $scope.title = cur_task.title;
    var taskDirective = "<" + cur_task.type + "taskdirective" + "></" + cur_task.type + "taskdirective" + ">";
    $scope.showTask = $sce.trustAsHtml(taskDirective);
    $compile(taskDirective)($scope);

});

Directive:

app.directive('clicktaskdirective', function () {
    return {
        restrict: 'E',
        template: '<ion-content style="padding: 20px;" class="text-center"><br><br><h1>{{taskTitle}}</h1><br><br><h4>{{taskText}}</h4><br><br><button class="button button-block button-stable" ng-click="start()">Stimmt!</button></ion-content>', 
        controller: function ($scope, $state, $stateParams) {

            console.log("This is showing up!")
        }
    }
});

HTML:

<div ng-bind-html="showTask"></div>

来源:https://stackoverflow.com/questions/38224198/angular-directive-doesnt-render-the-template-after-adding-html-dynamically-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!