How to pass a data object to a dynamically created directive? (fiddle)

允我心安 提交于 2019-12-14 02:29:12

问题


What I want to do is pretty simple, here is a fiddle to illustrate what I would like to achieve: code here: http://jsfiddle.net/vxpc1dry/

Basically on data fetch success I want to pass the returned response data object to a dynamically compiled directive, only the data object seems to be undefined not matter what :(

Any idea what I'm doing wrong? thanks a lot!


回答1:


angular.element(document.getElementById('dirContainer')).append($compile("<my-dynamic-directive name='data.name' data='data'></my-dynamic-directive>")(scope));

use two way = or one way & binding instead of text @ binding

myApp.directive("myDynamicDirective", function () {
    return {
        restrict: "E",
        scope: {
            data: "=",
            name: "="
        },
        template: "<div class='dynadir'><div>hello {{name}} <-- OK</div>"+
        "<div>hello {{data.name}} <-- WTF?</div></div>",    // <- undefined why?
        link: function (scope, element, attr) {
            console.log("The data passed: %O", scope.data); // <- undefined why?
        }
    }
});

http://jsfiddle.net/vxpc1dry/7/



来源:https://stackoverflow.com/questions/26093809/how-to-pass-a-data-object-to-a-dynamically-created-directive-fiddle

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