Answer the question
In order to leave comments, you need to log in
How to pass a parameter to the templateUrl directive?
Good afternoon.
Does anyone know how to pass a variable value to templateUrl?
Directive piece:
return {
restrict: 'A',
scope: {
message: '=mainMessage',
typeMessage: '=typeMessage'
},
templateUrl: function(elem, attrs) {
return 'template/common/' + attrs.typeMessage + '-message.tpl.html';
},
link: function(scope, element, attrs) {
}
<div class="mess" main-message="message" type-message="message.type"
ng-repeat="message in messages track by message.id">
</div>
Answer the question
In order to leave comments, you need to log in
in your case only ng-include. The variable must be interpolated prior to calling the compile directive.
Thank you, I took the advice of Sergei Protko and did so.
piece of directive
return {
template: '<ng-include src="getTemplateUrl()"/>',
scope: {
message: '=mainMessage',
typeMessage: '=typeMessage'
},
restrict: 'A',
controller: function($scope) {
$scope.getTemplateUrl = function() {
return 'template/common/' + $scope.typeMessage + "-message.tpl.html";
}
},
link: function(scope, element, attrs) {
}
}
Just did it recently.
Calling a function on templateUrl is somewhat misleading, as you cannot call this function and, accordingly, call it with any parameters you need. Therefore, the creation of a dynamic form for the directive, depending on external parameters, must be done in the link:
// тут можете сгенерировать любой вид директивы:
function getForm( obj, настройки){
str = '<a title="открыть в новом окне" ng-href="contragents.admin.html#нси_ключ={{obj._source.нси_ключ}}" target="_blank" ng-show="obj._source._$editor_props.menu_type==1" class="btn btn-xs btn-primary"><span ng-show="obj._source._$editor_props.bool_change==true" class="glyphicon glyphicon-asterisk" style="color:rgb(255, 183, 100)"></span> <u>'+obj._index+'-'+obj._type+'</u> <sup><i class="fa fa-external-link"></i></sup> </a>';
return str;
}
return {
scope: { ... },
// templateUrl: - убираем вообще
link: function(scope, element, attrs) {
var str_template = getForm(scope.obj, scope.настройки );
// надеюсь это не сложно:
var dom_node = $compile(str_template)(scope);
element.append(dom_node);
}
};
I did this in my project:
App.controller('formCtrl', ['$scope', function ($scope) {
$scope.order = {
md5hash:"036e4ab264;3253465a34535234524635f353",
name:'Введите имя',
phone:'Укажите свой номер',
email:'Ваш почтовый ящик',
address:""
};
}]).directive('formorder', [function() {
return {
templateUrl: 'tpl/formOrder.html',
replace: true,
scope:{
},
link: function ($scope, $element, $attrs){
$scope.order = $scope.$parent.order;
}
}
}]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question