'=?'是什么意思?在angularJS指令中隔离范围声明?

Nik*_*ita 124 angularjs angularjs-directive

等号后的问号是否有特殊含义?即:

scope: {foo: '=?'}
Run Code Online (Sandbox Code Playgroud)

如果'foo'无法解决,上述意思是'不引发错误?

Mat*_*ert 152

是:

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

==attr- 在本地范围属性和通过attr属性值定义的名称的父范围属性之间设置双向绑定.如果未attr指定名称,则假定属性名称与本地名称相同.给定 <widget my-attr="parentModel">和窗口小部件定义scope: { localModel:'=myAttr' },然后窗口小部件范围属性localModel将反映parentModel父范围的值.任何更改 parentModel都将反映在内,localModel并且任何更改 localModel都会反映出来parentModel.如果父作用域属性不存在,则会抛出NON_ASSIGNABLE_MODEL_EXPRESSION异常.您可以使用=?=?attr为了将属性标记为可选来避免此行为.

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

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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 它似乎只在设置值时抛出错误,如下所示:http://plnkr.co/edit/OSpaC6sPE0hY9yAeFghr?p = preview (7认同)
  • 虽然我个人希望它直接记录在范围部分而不是$ compile中. (3认同)