AngularJs-如何访问控制器&x27;指令中的函数与模型
嗨,我已经执行了很多次指令,每次我都必须像这样绑定每个属性、函数等AngularJs-如何访问控制器&x27;指令中的函数与模型,angularjs,Angularjs,嗨,我已经执行了很多次指令,每次我都必须像这样绑定每个属性、函数等 app.directive('postJobWizard', function () { return { restrict: 'EA', scope: { 'artistSearchModel': "=", 'showDropdown': "=", 'operaticRoleSearchResult': "=", 'viewAllJobs': "
app.directive('postJobWizard', function () {
return {
restrict: 'EA',
scope: {
'artistSearchModel': "=",
'showDropdown': "=",
'operaticRoleSearchResult': "=",
'viewAllJobs': "&",
'getSingleJob':"&",
'musicalWorkSearchResult': "=",
'institutionSearchResult': "=",
'changeTypeOfFilter': "&",
'searchArtistJob': "&",
'placeOfSearch':"@placeOfSearch",
'artistFilterSearch':'=',
'dropDownTitles':'=',
'searchPlaceHolder':'@searchPlaceHolder',
'isArtistOrAos':'=',
'noResultFound' : '=',
'oppurtunityOrBoard':'@',
'oppurtunitiesOrBoards':'@',
'showInfoIcon' : '=',
'infoIconText' : '='
},
controller: function () { },
controllerAs: '$postJob',
bindToController: true,
templateUrl: '/views/tss/directives/postJobWizard.html'
};
}))
但是假设我有50个函数和100个模型,我必须在
范围内编写它们。是否有任何方法或最佳实践可供我们访问父控制器功能和模型。请解释一下这是否是最佳做法?像这样注入常数:
app
.constant('scopeSettings',{your scope object goes here (probably on a seperate file)})
.directive('postJobWizard', function (scopeSettings) {
return {
restrict: 'EA',
scope: scopeSettings,
controller: function () { },
controllerAs: '$postJob',
bindToController: true,
templateUrl: '/views/tss/directives/postJobWizard.html'
};
});
我已经做了一个小片段,希望能给你指明正确的方向。还可以查看链接以了解有关指令以及作用域和控制器如何工作的更多信息
(函数(){
"严格使用",;
角度模块('myApp',[]);
角度.module('myApp').controller('MyController',MyController);
MyController.$inject=[];
函数MyController(){
var main=这个;
main.thisFunctionIsPublic=thisFunctionIsPublic;
函数thisFunctionIsPublic(){
return“此文本直接来自父控制器!”;
}
函数thisFunctionIsPrivate(){
返回“private!”;
}
}
角度.module('myApp')。指令('myDirective',函数(){
返回{
限制:'E',
范围:假,
控制器:“$controller”,
//bindToController:对,
模板:“公共函数:{{$controller.parentController.thisFunctionIsPublic()}}私有函数:{{{$controller.parentController.thisFunctionIsPrivate()}”,
控制器:函数($element){
this.parentController=$element.parent().controller();
console.log(this.parentController);
}
};
});
}());代码>
我认为50个函数和100个模型从一开始就是错误的设计,但在你的问题上,你可以在常量中添加{'showInfoIcon':'=','infoIconText':'=',..其余绑定}并加载它