AngularJS和生成的元素 [英] AngularJS and generated elements

查看:136
本文介绍了AngularJS和生成的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些第三方的JavaScript正在生成一些HTML元素的组件。我想与AngularJS情侣吧。

We have some third party javascript components that are generating some html elements. I'd like to couple it with AngularJS.

我已经试过这code

I've tried this code

<div ng-controller="ExpensesCtrl">
    <form id="expensesform">
        <input type="text" ng-model="expense.name" />
        <input type="text" ng-model="expense.amount" />
    </form>
    <button ng-click="add()">Add</button>expense | json
</div>


function ExpensesCtrl($scope) {

    $scope.expense = {};
    $scope.add = function () {
        $("#expensesform").append("<input type='text' ng-model='expense.age' />");
    };
}

http://jsfiddle.net/tChNh/

,但它不象例外工作

是否有机会得到这个工作?

Is there any chance to get this working ?

推荐答案

这是错误的方式做到这一点,但对于这个特殊的情况下,您会希望做这样的事情:的http://jsfiddle.net/wXZL7/1 。注入$编译服务。

This is the wrong way to do it, but for this particular scenario you'd want to do something like this: http://jsfiddle.net/wXZL7/1. Inject the $compile service.

此外,这是不是与角度做正确的方式。角的一句话是你不必乱用在你的控制器的DOM,让HTML和指令做到这一点。

Again, this isn't the right way to do it with Angular. Angular's thing is 'You don't have to mess with the DOM in your controller, let the HTML and directives do that'.

您想创建一个指令来包装无论是生成您的元素,让那做。阅读的例子指令指南: http://docs.angularjs.org/guide/directive

You want to create a directive to wrap whatever is generating your elements and let that do it. Read the directives guide for examples: http://docs.angularjs.org/guide/directive

这篇关于AngularJS和生成的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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