Dealing with DOM Manipulations in AngularJS

前端 未结 2 427
野趣味
野趣味 2020-12-02 11:14

When I perform DOM manipulation (add new HTML) using jQuery, AngularJS doesn\'t automatically detect variables in the new HTML and replace them with their values. For exampl

相关标签:
2条回答
  • 2020-12-02 11:44

    You have to inject $compile (http://docs.angularjs.org/api/ng.$compile) and use it so angular knows about the new html.

    $compile('<div>{{row}}</div')($scope).appendTo($event.currentTarget);

    However, it is frowned upon in angular to do DOM manipulation in your controllers. You want your controllers to handle business and your views to handle the view.

    Try a directive to do what you want. http://docs.angularjs.org/guide/directive

    0 讨论(0)
  • 2020-12-02 11:47

    If you use fragments for new elements (e.g. $("<" + "div>").appendTo("body")), using a wrapper like the following for the JQuery prepend/append methods lets you avoid having to change your element-adding code:

    // run angular-compile command on new content
    // (also works for prependTo/appendTo, since they use these methods internally)
    var oldPrepend = $.fn.prepend;
    $.fn.prepend = function()
    {
        var isFragment = arguments[0][0] && arguments[0][0].parentNode && arguments[0][0].parentNode.nodeName == "#document-fragment";
        var result = oldPrepend.apply(this, arguments);
        if (isFragment)
            AngularCompile(arguments[0]);
        return result;
    };
    var oldAppend = $.fn.append;
    $.fn.append = function()
    {
        var isFragment = arguments[0][0] && arguments[0][0].parentNode && arguments[0][0].parentNode.nodeName == "#document-fragment";
        var result = oldAppend.apply(this, arguments);
        if (isFragment)
            AngularCompile(arguments[0]);
        return result;
    };
    
    function AngularCompile(root)
    {
        var injector = angular.element($('[ng-app]')[0]).injector();
        var $compile = injector.get('$compile');
        var $rootScope = injector.get('$rootScope');
        var result = $compile(root)($rootScope);
        $rootScope.$digest();
        return result;
    }
    
    0 讨论(0)
提交回复
热议问题