Answer the question
In order to leave comments, you need to log in
AngularJS: How to dynamically connect a directive?
Good day!
The essence of the problem is as follows:
Information is loaded into modal windows using AJAX. I want to send the name of the directive to the controller by clicking on an element from the list. In the modal window that is being created, I assign the value of this directive to the element class, but the window opens empty. If you pre-register the name of the directive in the class, then the window will come filled. I searched for information, everywhere it is written about $compile, but I don’t understand how to attach it. In addition, everywhere in the examples everything is written in hardcode, all html is directly in the directive, and I want all html to be in html files, and JS to be JS.
I call the function of opening the modal window:
<span data-ng-click=showWindow("taxistoTablets")>Планшеты</span>
arm.controller("armController", function ($scope,$rootScope) {
$scope.jqxWindowSettings = {
maxHeight: 1000,
maxWidth: 1000,
minHeight: 350,
minWidth: 350,
height: 200,
width: 200,
resizable: true,
isModal: false,
autoOpen: false,
collapsed: true,
modalOpacity: 0
};
// show button click handler.
$scope.showWindow = function (pageName) {
$rootScope.jqxWindowSettings.apply('open');
$rootScope.pageName = pageName;
};
// Ok button click handler.
$scope.Ok = function () {
$rootScope.jqxWindowSettings.apply('close');
};
// cancel button click handler.
$scope.Cancel = function () {
$rootScope.jqxWindowSettings.apply('close');
};
});
});
arm.directive("tablets", function () {
return {
controller: "getInfo",
restrict: 'CA',
templateUrl: 'tablets/info-description.html',
scope: {
},
link: function ($scope, element, attributes) {
$scope.formName = 'Планшеты';
}
}
});
<jqx-window jqx-settings="jqxWindowSettings">
<div>
{{formName}}
</div>
<div>
<div data-ng-controller="armController" id="modalWindowBody">
<div data-ng-controller="getInfo">
<div class="{{pageName}}"></div>
</div>
</div>
<div>
<div style="float: right; margin-top: 15px;">
<jqx-button data-ng-click="Ok()" style="margin-right: 10px">Ok</jqx-button>
<jqx-button data-ng-click="Cancel()">Cancel</jqx-button>
</div>
</div>
</div>
</jqx-window>
Answer the question
In order to leave comments, you need to log in
Apparently, in my case, only the "bad option" is possible - $compile.
No matter how I tried to adapt the directive to my homeland in a different way, nothing came of it.
With the help of the official docks, I did the following:
Module declaration
var arm = angular.module("arm", ["jqwidgets"], function($compileProvider) {
$compileProvider.directive('compile', function($compile) {
return function(scope,element,attrs) {
scope.$watch(
function(scope){
return scope.$eval(attrs.compile);
},
function (value) {
element.html(value);
$compile(element.contents())(scope);
}
);
};
});
});
$scope.showWindow = function (pageName) {
$rootScope.jqxWindowSettings.apply('open');
$rootScope.html = ('<div class="' + pageName + '"></div>');
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question