angularJS $编译没有定义 [英] angularJS $compile is not defined

查看:380
本文介绍了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 {
        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.

这篇关于angularJS $编译没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆