javascript – 自定义指令中作用域对象内的嵌套对象

为什么我不能在我的范围对象中的嵌套对象中绑定,如下所示:

app.directive('myDirective', function() {
    return {
        scope: {
            dropdown: {
                option: '=selectedOption' //not working
            } 
        }
    }
})

我收到一个错误:

a.match is not a function

这是一个working plunker.

最佳答案 “为什么”的答案是“因为这不是它的工作原理”.

解析指令范围的AngularJS源代码在此处:https://github.com/angular/angular.js/blob/master/src/ng/compile.js#L829

  function parseIsolateBindings(scope, directiveName, isController) {
    var LOCAL_REGEXP = /^\s*([@&]|=(\*?))(\??)\s*(\w*)\s*$/;

    var bindings = {};

    forEach(scope, function(definition, scopeName) {
      var match = definition.match(LOCAL_REGEXP);

      if (!match) {
        throw $compileMinErr('iscp',
            "Invalid {3} for directive '{0}'." +
            " Definition: {... {1}: '{2}' ...}",
            directiveName, scopeName, definition,
            (isController ? "controller bindings definition" :
            "isolate scope definition"));
      }

      bindings[scopeName] = {
        mode: match[1][0],
        collection: match[2] === '*',
        optional: match[3] === '?',
        attrName: match[4] || scopeName
      };
    });

    return bindings;
  }

正如您所看到的,它只通过范围对象属性进行单次传递,并且不会递归地下降到对象属性中.

点赞