Add directive to module after bootstrap and applying on dynamic content

后端 未结 2 1820
走了就别回头了
走了就别回头了 2020-12-29 13:21

I have a web page with a module defined (myModule) where I\'m boostraping angularjs using

angular.bootstrap(element,[myModule.name]);

After

相关标签:
2条回答
  • 2020-12-29 14:12

    This Fiddle show-cases how to dynamically load/register and use:

    • Angular controllers (using $controllerProvider)
    • Angular directives (using $compileProvider)
    • Angular partial templates (using $templateCache)

    Code: Setup

    // Initialize app.lazyController and app.lazyDirective.
    // We will later use them to create controller and directives dynamically.
    var app = angular.module('app', []);
    app.config(function($controllerProvider, $compileProvider) {
            // see page 12 of:
            //    http://www.slideshare.net/nirkaufman/angularjs-lazy-loading-techniques
            app.lazyController = $controllerProvider.register;
    
            // see: http://jsfiddle.net/8Bf8m/33/
            app.lazyDirective = $compileProvider.directive;
        });
    
    // set of partials
    var partials = [];
    
    // store scope & templateCache, so we can dynamically insert partials
    var scope, templateCache;
    
    // define main controller
    function MainCtrl($scope, $templateCache) {
        $scope.partials = partials;
    
        scope = $scope;
        templateCache = $templateCache;
    }
    

    Code: Example

    var maxPartials = 3;
    var i = 0;
    var timer = setInterval(function() {
        var i = partials.length;
    
        app.lazyDirective('clickMe', function () { return {
            link : function (scope, element) {
                element.bind('click', function () {
                    alert('Clicked: ' + element.text());
                });
            },
        };});
    
        // define controller
        var ctrlName = 'partial' + i + 'Ctrl';
        app.lazyController(ctrlName, function($scope) {
            $scope.text = 'hi ' + i;
        });
    
        // add partial template that I have available in string form
        var newPartial = {
            name: 'template' + i,
            content: '<div ng-controller="' + ctrlName + '" class="a' + i + '">' +
            '<input type="text" ng-model="text"></input>'+
            '{{text}} <br/>' + 
            '<button click-me="">Click Me!</button>' +
            '</div> <br/> <br/>'
        };
        partials[i] = newPartial;
    
        // add template and notify angular of the content change
        templateCache.put(partials[i].name, partials[i].content);
        scope.$apply();
    
        // stop timer
        if (partials.length >= maxPartials)  clearInterval(timer);
    }, 1000);
    
    0 讨论(0)
  • 2020-12-29 14:21

    The thing with registering lazy controller or directives is that you have to get hold of $controllerProvider and $compileProvider respectively.

    It can be done only during configuration phase so you have to keep the references until you load the controllers/directives.

    Lately I was working on lazy loading of the controller, today I've added support for directives, check out my code here:

    https://github.com/matys84pl/angularjs-requirejs-lazy-controllers/

    particularly this module lazy-directives.js

    NOTE: I use RequireJS in my project, however applying my solution to yepnope should be quite easy.

    0 讨论(0)
提交回复
热议问题