AngularJS - 为什么叫NG-变化模型更新过吗? [英] AngularJS - Why is ng-change called before the model is updated?

查看:320
本文介绍了AngularJS - 为什么叫NG-变化模型更新过吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅以下plnkr: http://plnkr.co/edit/KZwIBX?p=$p $ PVIEW

第一标准选择按预期工作(至少据我预期/预期),这意味着模型更新后的NG-改叫。但是,当我创建一个具有隔离范围,它似乎是我的模型更新之前NG-变化被称为指令。显然,也有一些是我误解。

情况非常相似, AngularJS范围更新p>

解决方案

与在plnkr的例子的问题是要传递的改变功能,该分离范围为绑定到父范围使用的变化:'和;
通过使用&放大器;该函数将被评价为在父的上下文中一个前pression它将模型改变之前进行评估(如果没有结合到模型作为参数)

有可能更改为了工作的例子有两种方式:


    '='定义

  1. 您可以在变更指令定义范围隔离

  2. 您可以使用它,因为它是,但调用带有参数的父对象引用的功能。
    对于这里的第二个例子是一个很好的解释:
    <一href=\"http://www.thinkster.io/pick/oEo8ZAXv27/angularjs-isolate-scope\">http://www.thinkster.io/pick/oEo8ZAXv27/angularjs-isolate-scope


Please see the following plnkr: http://plnkr.co/edit/KZwIBX?p=preview

The first standard select works as expected(at least as far as I expected/anticipated), meaning that ng-change is called after the model is updated. But when I create a directive that has an isolate scope it would seem that ng-change is called before my model is updated. Obviously there is something I am misunderstanding.

Situation very similar to AngularJS scope updated after ng-change

解决方案

The problem with the example in the plnkr is that you are passing the change function, to the isolate scope as a binding to the parent scope by using change : '&'. By using the & the function will be evaluated as an expression in the context of the parent and it will be evaluated before model changed (if there is no binding to the model as a parameter)

There can be two ways to change the example in order to work:

  1. you can define your isolated scope in the directive with the change: '=' definition

  2. you can use it as it is but call the parent referenced function with a parameter. For the second example here is a great explanation: http://www.thinkster.io/pick/oEo8ZAXv27/angularjs-isolate-scope

这篇关于AngularJS - 为什么叫NG-变化模型更新过吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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