I'm new to angularjs and directives, and I'm actually having troubles with them. I want to create a custom directive to display images in my ionic 1 application.
To achieve this, I need to bind an object ('modal') to be able to link a showModal() function to my parent view; and also an url of the media that I want to display : so, an object and a string.
I use a templateUrl that I configure in my directive. The modal is correctly linked to my parent and template, but I cannot display my image : I get the right url in my scope, controller, but ng-src doesn't load my url (it's empty in my template). I think the issue is related to a bad understanding of the scope behaviour, but I cannot reach a solution.
If you could help me with this problem, I would be very thankful!
Here is some of my code for a better understanding of the problem :
Template :
<div class="modal image-modal transparent">
<button class="button button-full icon-right ion-close-round" id="button-modal"
ng-click="modal.hide()">Close</button>
<img ng-src="{{url}}" class="fullscreen-image"/>
</div>
Parent view :
<div ng-switch-when="image">
<a class="item item-icon-left" ng-click="vm.modal.show()">
<i class="icon ion-android-image"></i>
{{vm.media.filename}}
</a>
<image-displayer url="{{vm.media.url}}" modal="vm.modal"></image-displayer>
</div>
Directive :
.directive('imageDisplayer', ['$ionicModal', function($ionicModal) {
return {
scope: {},
restrict: 'E',
replace: true,
controller: function() {
console.log(this);
},
controllerAs: 'vm',
bindToController: {
modal: '=',
url: '@'
},
tempalteUrl: 'components/medias/image-popover.html',
link: function(scope, element, attr) {
console.log(scope);
var url = scope.url;
var modal = scope.vm.modal;
modal.show = showModal;
modal.close = closeModal;
function showModal() {
modal.show();
};
function closeModal() {
modal.hide();
};
$ionicModal.fromTemplateUrl('components/medias/image-popover.html', {
animation: 'slide-in-up'})
.then(function(modalCreated) {
modal = modalCreated;
});
}
};
Thank you in advance !
EDIT
So I found the solution to my problem : IonicModal creates its own scope, that's why it cannot access my data. To solve the problem I added :
$ionicModal.fromTemplateUrl('components/medias/image-popover.html', {
animation: 'slide-in-up'})
.then(function(modalCreated) {
modal = modalCreated;
modal.url = url; //ADDED THIS LINE
});
It can also be fixed by adding the controller in the html template or the scope as a parameter to the ionic modal in the controller.