AngularJS - 来自transclude范围的访问指令范围

amo*_*fis 5 angularjs angularjs-directive angularjs-scope

我有一个表格的指令.通常它只是我需要的,但有时我需要添加更多的输入字段.所以我尝试使用transclusion,但它不起作用.

我创建了一个清单来说明:http://plnkr.co/edit/zNOK3SJFXE1PxsvUvPBQ?p = preview

Directive是一个简单的表单,带有输入字段,transclusion和一个帮助测试它的按钮(不包括重要部分):

scope: {
},
transclude: 'element',
template: 
  '<form name="myForm">' +
  '<input type="text" ng-model="data.inDirective"></input>' +
  '<div ng-transclude></div>' +
  '<button ng-click="showData()">show data</button>' +
  '</form>'
Run Code Online (Sandbox Code Playgroud)

在这里,它与翻译一起使用:

<form-as-directive>
  <input type="text" ng-model="data.inTransclude"></input>
</form-as-directive>
Run Code Online (Sandbox Code Playgroud)

我可以以某种方式在转换中使用指令的范围吗?

cip*_*pak 7

如果需要将transcluded html中的控件绑定到指令的(isolate)范围,则必须使用link函数的transcludeFn参数"手动"(不使用ng-transclude)进行转换.此功能允许您更改转换的范围.

scope: {
},
transclude: 'element',
replace: true,
template: 
    '<form name="myForm">' +
    '<input type="text" ng-model="data.inDirective"></input>' +
    '<div class="fields"></div>' +
    '<button ng-click="showData()">show data</button>' +
    '</form>',
link: function (scope, elem, attrs, ctrl, transcludeFn) {
    // "scope" here is the directive's isolate scope 
    elem.find('.fields').append(transcludeFn(scope, function () {}));
}
Run Code Online (Sandbox Code Playgroud)

否则,转换将自动绑定到父(控制器)作用域的(新)子级,以便可以访问该父作用域的属性(通过继承).


Che*_*niv 1

看起来$$nextSibling这就是您所需要的:

 scope.$$nextSibling.data.inTransclude
Run Code Online (Sandbox Code Playgroud)

这里

当嵌入范围和隔离范围同时存在时,隔离范围属性 $$nextSibling 将引用嵌入范围。

普朗克: http: //plnkr.co/edit/z2Bmfx ?p=preview