【问题标题】:Angular.js directives compile function [closed]Angular.js指令编译功能[关闭]
【发布时间】:2013-12-09 15:55:07
【问题描述】:

大家好,

我有一个小问题,谁能写一些简单的例子 使用编译函数并在指令中进行一些解释

【问题讨论】:

    标签: javascript angularjs frontend


    【解决方案1】:

    我实际上是wrote a blog entry about exactly this

    它详细介绍了它的工作原理......但基本上,它的使用方式如下:

    // take some HTML
    var html = '<div><h2>Some HTML</h2><p ng-repeat="item in items">{{item.name}}</p></div>';
    
    // wrap it in an element
    var element = angular.element(html);
    
    // compile it as a view with $compile
    var compiledView = $compile(element);
    
    // create a scope (if you don't already have one)
    var $scope = $rootScope.$new();
    $scope.items = [
        { name: 'Test Monkey', id: 1 },
        { name: 'Bob Hope', id: 2 }
    ];
    
    // pass that scope into the compiled view 
    // to apply that scope to the view
    compiledView($scope);  
    
    // Now all of your directives are wired up and bound to the scope you passed!
    

    注意:你真的不应该在指令之外使用它。

    【讨论】:

    • 是否可以编译已经在 DOM 上渲染的元素?
    【解决方案2】:

    我建议您阅读THIS 系列帖子。它准确地显示了您正在寻找的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 2015-02-13
      • 2014-12-10
      • 1970-01-01
      相关资源
      最近更新 更多