How to add Angular-enabled elements to DOM?

后端 未结 3 757
谎友^
谎友^ 2021-02-06 02:26

I would like to add some Angular-enabled DOM elements programmatically. Actually, I probably will need to add custom components. How can I do it?

Here\'s a trivial fiddl

3条回答
  •  遇见更好的自我
    2021-02-06 02:46

    Although I am not entirely sure what the desired outcome is, you do want to be sure that all DOM manipulation is done within a directive and not inside your controller.

    This JSfiddle example should get you going in the right direction.

    http://jsfiddle.net/ZJSz4/5/

    angular.module("main", []).controller("MyCtrl", function($scope) {
        $scope.test = 'Test Message';
    }).directive("ngPortlet", function ($compile) {
    return {
        template: '
    {{test}}
    ', restrict: 'E', link: function (scope, elm) { scope.add = function(){ console.log(elm); elm.after($compile('')(scope)); } } }; });

提交回复
热议问题