“="是什么意思?在 angularJS 指令中隔离范围声明? [英] what's the meaning of '=?' in angularJS directive isolate scope declaration?

查看:29
本文介绍了“="是什么意思?在 angularJS 指令中隔离范围声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

等号后面的问号有特殊含义吗?即:

Does the question mark after equals have special meaning? ie:

scope: {foo: '=?'}

上面的意思是如果 'foo' 无法解析,则不引发错误?

does the above mean 'do not raise error if 'foo' cannot be resolved?

推荐答案

是:

isolate"作用域采用一个对象哈希,它定义了一组从父作用域派生的局部作用域属性.这些本地属性对于模板的别名值非常有用.本地定义是本地范围属性到其源的散列:

The 'isolate' scope takes an object hash which defines a set of local scope properties derived from the parent scope. These local properties are useful for aliasing values for templates. Locals definition is a hash of local scope property to its source:

==attr - 在本地作用域之间设置双向绑定属性和通过值定义的名称的父作用域属性attr 属性.如果未指定 attr 名称,则假定属性名称与本地名称相同.给定的范围的小部件定义:{localModel:'=myAttr' },那么小部件范围属性 localModel 将在父作用域上反映 parentModel 的值.任何更改parentModel 将反映在 localModel 和任何更改localModel 将反映在 parentModel 中.如果父作用域属性不存在,它会抛出一个NON_ASSIGNABLE_MODEL_EXPRESSION 异常.你可以避免这种行为使用 =?=?attr 将属性标记为可选.

= or =attr - set up bi-directional binding between a local scope property and the parent scope property of name defined via the value of the attr attribute. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given <widget my-attr="parentModel"> and widget definition of scope: { localModel:'=myAttr' }, then widget scope property localModel will reflect the value of parentModel on the parent scope. Any changes to parentModel will be reflected in localModel and any changes in localModel will reflect in parentModel. If the parent scope property doesn't exist, it will throw a NON_ASSIGNABLE_MODEL_EXPRESSION exception. You can avoid this behavior using =? or =?attr in order to flag the property as optional.

它应该在影响范围属性的每个摘要上触发预期错误:

It should trigger the expected error on every digest that affects the scope property:

parentSet = parentGet.assign || function() {
// reset the change, or we will throw this exception on every $digest
lastValue = scope[scopeName] = parentGet(parentScope);
     throw Error(NON_ASSIGNABLE_MODEL_EXPRESSION + attrs[attrName] +
     ' (directive: ' + newScopeDirective.name + ')');
};

//...


if (parentValue !== scope[scopeName]) {
    // we are out of sync and need to copy
    if (parentValue !== lastValue) {
        // parent changed and it has precedence
        lastValue = scope[scopeName] = parentValue;
    } else {
        // if the parent can be assigned then do so
        parentSet(parentScope, lastValue = scope[scopeName]);
    }
}

这篇关于“="是什么意思?在 angularJS 指令中隔离范围声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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