JavaScript Insert HTML (user-defined directives) into view - AngularJS html user insert image

This is my first question on Stackoverflow so please excuse any mistakes I make. I'm trying to display in the view, some HTML from a template, which I use in the following directive:

app.directive('picturegallery', function () {
return {
    scope: true,
    restrict: "E",
    templateUrl: '../../Controllers/pictureGallery.html'
}

});

I do this on click by applying .push, so <picturegallery></picturegallery> gets added to the $scope.sections:

app.directive("picgallery", function ($compile) {
return function (scope, element, attrs) {
    element.bind("click", function () {
        scope.count++;
        scope.sections.push({ desc: "<picturegallery></picturegallery>" });
        angular.element(document.getElementById('template-id')).append($compile("<picturegallery></picturegallery>")(scope));

    });
};

});

I use $sce.parseAsHtml as shown below and it works, but the HTML still doesn't get translated to the actual HTML pointed by the template, and simply shows up as <picturegallery></picturegallery>. I would like the actual template pointed to by pictureGallery.html to show up.

$scope.sections = [
        { desc: '<div>hello</div>' },
        { desc: '$sce.parseAsHtml("<picturegallery></picturegallery>")' }

    ];

Is there any way I could show up the actual content from the template? I've already been through a couple of solutions like Angularjs: preview sanitized html on Stackoverflow which suggested the use of $sce.parseAsHtml and $sce.trustAsHtml but they don't work for my code.

Answer:1

The best solution for me with a similar problem was to create a custom filter which allows me to render imported HTML.

app.filter('unsafe', ['$sce', function($sce) { return $sce.trustAsHtml; }])

In my views I can then display the HTML with the filter:

<div>{{myCtrl.someHTML | unsafe}}</div>

If this doesn't work out of the box, you'll also need to set a resource white list in your angular config like this:

$sceDelegateProvider.resourceUrlWhitelist(['self', 'https://www.google.com/maps/embed/v1/**']);

(The google link is only to demonstrate that you can import external HTML from another website/service and render it as a trusted source)

Answer:2

I'm trying to open a remote video (let's say it's located at http://www.example.com/video.mp4) with the default Android player launched directly from Google Chrome, making use of the brand new intent:/...

I'm trying to open a remote video (let's say it's located at http://www.example.com/video.mp4) with the default Android player launched directly from Google Chrome, making use of the brand new intent:/...

  1. open video react native

This code works fine on IE, but fails on Chrome. Theory: When you click on the input, the input marks with an X or when you hit again the X is deleted (just like a checkbox) , when any of these ...

This code works fine on IE, but fails on Chrome. Theory: When you click on the input, the input marks with an X or when you hit again the X is deleted (just like a checkbox) , when any of these ...

  1. javascript code works with alert
  2. javascript code only works in debug
  3. how javascript code works
  4. javascript code only works with alert
  5. javascript code doesn't work
  6. javascript code doesn't work in chrome
  7. javascript code won't work
  8. javascript code dont work
  9. how javascript code coverage works

In a web application I have lists of things with the following structure: As you can see, when we list items (users, roles or anything basically), we have some associated actions on the right, ...

In a web application I have lists of things with the following structure: As you can see, when we list items (users, roles or anything basically), we have some associated actions on the right, ...

  1. accessibility links in tables
  2. accessibility for links

I'm writing a web application in JavaScript, using Bootstrap. It dynamically creates several textareas, and I want to convert them to CKEditors. However, only one instance of CKEditor is created, the ...

I'm writing a web application in JavaScript, using Bootstrap. It dynamically creates several textareas, and I want to convert them to CKEditors. However, only one instance of CKEditor is created, the ...