Angular.js指令编译函数

时间:2013-12-09 15:55:07

标签: javascript angularjs frontend

大家好,

  

我有一个小问题,任何人都可以写一些简单的例子   使用编译函数和指令中的一些解释

2 个答案:

答案 0 :(得分:2)

我实际上是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!

注意:您不应该在指令之外使用它。

答案 1 :(得分:0)

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

相关问题