如果我在指令模板中有一个 div,其属性值为 ng-controller,我该如何将其更改为变量:
original html : <div ng-controller="HomeController"> ..... </div>
directive: <my-directive some-var-name="HomeController"> ....</my-directive>
.directive('MyDirective', function(){
return {
.....
scope:
{
someVarName : "????" <-- what type? "=", "@", "&" : none seem to work
},
template: '<div ng-controller="someVarName">.....</div>'
}
});
感谢马克解决这个问题:
<select-add-new select-model="$parent.selectedFrontAxle" text="add new"
select-phrase="Front Axle Type" preselected-filter="Front"
label-name="Front Axle" open-dialog="Front"
select-options="axleTypes" var-ctrl="AxleTypesCtrl"></select-add-new>
.directive('selectAddNew', function () {
return {
replace: true,
restrict: "E",
scope: {
selectModel: "=",
selectOptions:"=",
labelName: "@",
preselectedFilter: "@",
selectPhrase: "@",
text: "@"
},
compile: function(tElement, attrs) {
var div = tElement.find('#ctrlId');
div.attr('ng-controller', attrs.varCtrl);
},
template: '<div>' +
'<div class="local-label">{{labelName}}: </div>' +
'<name-value-select-control select-phrase="{{selectPhrase}}" selected-item="selectModel" preselected-filter="{{preselectedFilter}}" options="selectOptions"></name-value-select-control>' +
'<div id="ctrlId">' +
'<div txt-add-new text="{{text}}" action="openDialog(\'Front\')" style="display: inline-block"></div>' +
'</div>' +
'</div>'
};
})
- - - - - - - - - - - - - - - - - - - - - - -标记 - - --------------------------------------------------
这是笨蛋:
http://plnkr.co/edit/b6G2y3yqjhxpu059ZrWB
这是问题所在:
我将两个指令组合成一个超级指令。如果您转到 app.js,指令“selectAddNew”,从底部开始的第三行,您将看到:
......... action="openDialog(\'Front\')" ......
但这是硬编码的。我尝试了各种方法让它成为超级指令中的一个动作,但我最接近的是打开整个表单窗口的通用对话框。有任何想法吗?