使用模板的指令中更新NG-模型 [英] Updating ng-model within a directive that uses a template

查看:84
本文介绍了使用模板的指令中更新NG-模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实例化一个像这样的指令:

I have a directive that is instantiated like this:

<datepicker ng-model="foo"></datepicker>

里面的指令,日期选择器标签此模板替换为:

Inside the directive, the datepicker tag is replaced by this template:

template: '<div class="datepicker-wrapper input-append">' +
                     '<input type="text" class="datepicker" />' +
                     '<span class="add-on"><i class="icon-calendar"></i></span>' +
                   '</div>'

这是我想要绑定到NG-模型的值是输入字段的值。什么是去最好的办法,所以我保持NG-模型的数据双向绑定?

The value that I want ng-model bound to is the value of the input field. What is the best way to go about this so I maintain the two-way data binding of ng-model?

推荐答案

根据您的直通是多么复杂,你可以只使用=范围做一个本地名称和ngModel之间的双向绑定,就像这个小提琴:

Depending on how complicated your passthrough is, you can just use the = scope to do a bidirectional bind between a local name and ngModel, like in this fiddle:

http://jsfiddle.net/mThrT/22/

我有一个时间地狱设立小提琴出于某种原因(第一次与角度尝试),但这里的钱拍:

I had a hell of a time setting up the fiddle for some reason (first time trying with angular) but here's the money shot:

template: '<div class="datepicker-wrapper input-append">' 
        + '<input type="text" class="datepicker" ng-model="bar" />' 
        + '<span class="add-on"><i class="icon-calendar"></i></span>' 
        + '</div>',
scope: {
    bar: '=ngModel'
},

这篇关于使用模板的指令中更新NG-模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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