angularJS $编译没有定义定义、angularJS

由网友(夜半花落〃心微凉)分享简介:我想学习AngularJS,我试图动态编​​译一些DOM元素...我已经尝试了演示:I'm trying to learn AngularJS and I'm trying to dynamically compile some DOM elements... I've tried the demo:try {v...

我想学习AngularJS,我试图动态编​​译一些DOM元素...我已经尝试了演示:

I'm trying to learn AngularJS and I'm trying to dynamically compile some DOM elements... I've tried the demo:

try {
        var templateHTML = angular.element('<p>{{total}}</p>'),
            scope = ....;

        var clonedElement = $compile(templateHTML)(scope, function(clonedElement, scope) {
          //attach the clone to DOM document at the right place
        });

        //now we have reference to the cloned DOM via `clone`
} catch (ex) {
alert(ex.message);
}

但我得到的回复是一个$编译没有定义

but all I get back is a "$compile is not defined"

HELP!

推荐答案

你在哪里调用这个$ C $从C?它是安全的假设它超出了角框架您使用angular.element的(...)?

Where are you calling this code from? Is it safe to assume it is outside of the Angular framework by your use of angular.element(...)?

如果是这样,你可以使用这个:

If so, you can use this:

// Split across two lines for readability...
angular.element(<something within Angular's scope>)
    .injector().get('$compile')(...)

如果没有,你可能只需要注入$编译到控制器/指令/服务。

If not, you may simply need to inject $compile into the controller/directive/service.

阅读全文

相关推荐

最新文章