【发布时间】:2013-12-09 15:55:07
【问题描述】:
大家好,
我有一个小问题,谁能写一些简单的例子 使用编译函数并在指令中进行一些解释
【问题讨论】:
标签: javascript angularjs frontend
我有一个小问题,谁能写一些简单的例子 使用编译函数并在指令中进行一些解释
【问题讨论】:
标签: javascript angularjs frontend
我实际上是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!
注意:你真的不应该在指令之外使用它。
【讨论】:
我建议您阅读THIS 系列帖子。它准确地显示了您正在寻找的内容。
【讨论】: