带有ng-include模板的嵌套指令

时间:2014-02-20 22:24:38

标签: javascript html angularjs

我有一个嵌套指令的页面,嵌套可能是无限的。我想确保在执行其他操作之前加载所有模板。我试图访问我的函数“registerLoad”,但我的范围。$ parent.hasOwnProperty('registerload')保持'false',我认为我有一个范围问题。谢谢你的帮助。

<div inbook-include="template/1.html"></div>
<div inbook-include="template/2.html">
    <div inbook-include="template/2-1.html">
        <div inbook-include="template/2-1-1.html">
        </div>
    </div>
    <div inbook-include="template/2-2.html">
    </div>
</div>


app.directive('inbookInclude', function (inbookConstants, $log, templateLoader, $timeout, $interpolate) {

    // ID of parent
    var parentId;

    function registerLoad(scope) {
        if (scope.$parent.hasOwnProperty('registerload')) {
            parentId = scope.$parent.registerLoad(scope);
            console.log('I got a dad too');
        } else {
            console.log('all have been loaded');
        }
    }

    return {
        restrict: 'A',
        replace: true,
        template: '<div ng-include="url.address" onload="onLoad()">,
        scope: true,
        compile: function compile(iElement, iAttrs) {
            var urlTemplate = iAttrs.inbookInclude;
            return {
                pre: function prelink(scope, iElement, iAttrs) {
                    scope.url = {};
                },
                post: function postlink(scope, iElement, iAttrs) {


                    scope.$on('loaded', function (event, childId) {
                            console.log('one of my kids loaded', event, childId);
                    }),


                scope.onLoad = function () {
                    if (scope.$parent) {
                        if (scope.$parent.hasOwnProperty('registerload')) {
                            scope.$parent.registerLoad(scope.$id);
                            console.log('my dad can register me');
                        }
                        console.log('my dad cannot register me', scope.$parent);
                        console.log(getMethods(scope.$parent));

                    }
                    scope.$broadcast('reveal');
                }

                if (scope.$eval(iAttrs.forceLoad)) {
                    init();
                }

                templateLoader.onReveal(iElement.parent(), scope, function () {
                    init();
                    scope.$emit('loaded', scope.$id);
                });


                function init() {
                //
                //
                }

            }

        }
    }
}
});

0 个答案:

没有答案