触发NG-模式。$格式化以编程方式运行 [英] Trigger ng-model.$formatters to run programatically

查看:144
本文介绍了触发NG-模式。$格式化以编程方式运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ngModel。$格式化 ,能够在一台服务器负载的依赖,尽快格式的数据。对于我来说,它需要加载一个查找表去从一种ID来另一个。 $ modelValue 商店一件事 $ viewValue 显示另一个。 pretty直接的东西。

诀窍是,如果没有加载我的查找表,我不能做格式化成$ viewValue。

在我的数据负载,我需要做到以下几点:


  • ngModel。$ formatters.push(myFormatter)

  • 告诉ngModel,开始从 $ modelValue管道 - > $格式化 - > $ viewValue

$渲染()不工作,这只是移动从 $ viewValue 的值到UI控件。
$ rollbackViewValue()看起来很有希望,但是这只是一个不稳定的版本(的 1.3.0-beta.18 )。

code样品:<​​/ P>

  mappingTable.load()。然后(功能(数据){
  mappingData =数据;
  ngModel $ formatters.push(myFormatter)。 //需要mappingData以功能
  // TODO:告诉ngModel运行到$ $现有的格式化modelValue计算新的$ viewValue和$使其
  //ngModel.$render()//不工作,只放$ viewValue中的DOM元素。
});


解决方案

纵观code代表ngModelController,看来你偶然发现(设置 $ modelValue 到任何东西比目前的实际模型值等)是这样做的接受的方式。正如你所说,你设置的值不使用:它只是触发更新。首先检查其电流值,以确保它实际上改变(或使用的可能性极小值)。

 如果(ngModel。$ modelValue =='巴')
    ngModel $ modelValue ='富'。
其他
    。ngModel $ modelValue ='吧';

下面是一个<一个href=\"http://stackoverflow.com/questions/13995414/have-formatters-be-called-when-view-is-first-populated\">related问题。

此外,还有看起来像一个正式的积极拉动申请 这样做的方式是即将到来的。

它的工作原理的原因是ngModelController设置了一个 $观看运行每个消化周期比较 $ modelValue 该值NG-模式势必。如果它们不匹配时,会触发 $格式化管道。

I'd like a custom control that uses ngModel.$formatters to be able to format data as soon as a server dependency loads in. In my case, it needs to load a lookup table to go from one kind of id to another. $modelValue stores one thing $viewValue displays another. Pretty straight-forward stuff.

The trick is that if my lookup table isn't loaded, I can't do the formatting into a $viewValue.

Once my data loads, I need to do the following:

  • ngModel.$formatters.push(myFormatter)
  • Tell ngModel to start the pipeline from $modelValue -> $formatters -> $viewValue

$render() doesn't work, this just moves the value from $viewValue into the UI control. $rollbackViewValue() looks promising, but that's only in an unstable version (1.3.0-beta.18).

Code Sample:

mappingTable.load().then(function(data){
  mappingData = data;
  ngModel.$formatters.push(myFormatter); // needs mappingData in order to function
  // TODO: Tell ngModel to run the existing $modelValue through $formatters to calculate a  new $viewValue and $render it
  //ngModel.$render() // doesn't work, only puts the $viewValue in the DOM element.
});

解决方案

Looking at the code for ngModelController, it appears that what you stumbled upon (setting $modelValue to anything other than the current actual model value) is the accepted way to do this. As you say, the value you set is not used: it just triggers the update. Check its current value first to make sure it actually changes (or use a very unlikely value).

if (ngModel.$modelValue == 'bar')
    ngModel.$modelValue = 'foo';
else
    ngModel.$modelValue = 'bar';

Here is a related question.

Also, there is an active pull request that looks like an "official" way of doing this is forthcoming.

The reason it works is that ngModelController sets up a $watch that runs every digest cycle that compares $modelValue to the value that ng-model is bound to. If they don't match, it triggers the $formatters pipeline.

这篇关于触发NG-模式。$格式化以编程方式运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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