Answer the question
In order to leave comments, you need to log in
How to load a service on demand?
Can I take the attribute values in the directive and add the service as a dependency in the controller?
If so, how?
Thank you in advance
Code
angular.module('tableData', [])
.directive('tableData', tableData)
// ################# Логика ################# //
function tableData() {
return {
restrick: 'E',
replace: true,
transclude: true,
template: require('../../template/directives/tableData.html'),
controller: TableCtrl,
controllerAs: 'table',
bindToController: {
type: '@'
}
}
}
function TableCtrl($scope, $filter) {
var wm = this;
wm.type // Название
}
angular.module('tableData', [])
.directive('tableData', tableData)
.service('includeService', function(service1, service2){
methods = {
service1: oneService,
service2: otherService
}
return methods
//#######################
function oneService() {
return service1
}
function otherService() {
return service2
}
})
// ################# Логика ################# //
function tableData() {
return {
restrick: 'E',
replace: true,
transclude: true,
template: require('../../template/directives/tableData.html'),
controller: TableCtrl,
controllerAs: 'table',
bindToController: {
type: '@'
}
}
}
function TableCtrl($scope, $filter, includeService) {
var wm = this;
wm.type // Название
includeService[wm.type]
}
//################# Конструктори ################# //
Answer the question
In order to leave comments, you need to log in
make stateless directives and life will be a little easier without these perversions. Does the directive need data? Throw them outside through the attributes. Introduce yourself such a simple rule that directives do not receive state themselves through services. And the services should be used by the directive only to update the state of the data outside, but again, they themselves do not change anything.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question