Ionic SwipeCards, $scope.$parent issue? Please help!

So I’m implementing an app using the SwipeCards demo (https://github.com/driftyco/ionic-contrib-swipe-cards/blob/master/example/www/js/ionic.swipecards.js)

there is a scope issue I haven’t been able to solve. In one directive there is this line:
$scope.$parent.swipeCard = swipeableCard;

I can see it is set in the console, but later in this factory it is undefined:

.factory(’$ionicSwipeCardDelegate’, [’$rootScope’, function($rootScope) {
return {
popCard: function($scope, isAnimated) {
$rootScope.$emit(‘swipeCard.pop’, isAnimated);
},
getSwipebleCard: function($scope) {
return $scope.$parent.swipeCard;
}
}
}])

I’m not sure how to access it, please help!

Ok I’m not sure this is the correct/Angular way to do this, but here is my patch:

replace :
angular.module(‘ionic.contrib.ui.cards’, [‘ionic’])
.directive(‘swipeCard’, [’$timeout’, function($timeout) {
with:
angular.module(‘ionic.contrib.ui.cards’, [‘ionic’])
.directive(‘swipeCard’, [ ‘$timeout’, ‘$rootScope’, function ($timeout, $rootScope)

replace :
$scope.$parent.swipeCard = swipeableCard;
with :
$rootScope = swipeableCard;

replace :
getSwipebleCard: function($scope) {
return $scope.$parent.swipeCard;
}
with :
getSwipebleCard: function($scope) {
return $rootScope.swipeCard;
}

Good luck!

In general, you should be very cautious about accessing $rootScope, and particularly cautious when replacing a reference to $scope.$parent with $rootScope.

$scope.$parent may differ for every instance of the SwipeCard directive you employ. But there’s exactly one $rootScope, so you risk different instances stamping all over each other’s state.

If you only have one SwipeCard active at a time, and they are cleanly created / destroyed, you’ll probably get away with it, but it’s still a dangerous practice.

My rule of thumb is that if I start off thinking “I have a change that uses $rootScope that will be useful”, I still consider it.

However if I hit a dead end and think “I know, I’ll just use $rootScope” it usually means I need to think about it some more.

I appreciate the advice. $rootScope is already in use by this script, so I figured I would try it and tada it worked. Beats not working which was the original state.

The person who wrote the original script is also a contributor to Ionic so I hoped he might chime in. It looks like it may have been abandoned. Anyways, I’m new to ng if you have a better way please be my guest! Saying maybe I should think about it some more is kinda a cop out. I may think about it and never find a better way.

Well it might be less of a cop out if you described what you were trying to achieve :smile:

Aside from a small fragment of code, there’s no way of knowing what the purpose of your code is and how it’s currently failing to do what you want it to.

It might be the change you’ve made is actually the only way to get there, but it’s hard to evaluate.

There is a lot to be said for having something you can demonstrate, no arguments from me.

Yes you are correct sir.

I apologize, my frustrations are my own I should not blame others!

I think now that my approach was wrong, I was using 3 controllers, “CardCtrl” “CardsCtrl” and my own "BtnsCtrl"
In which I have 2 buttons to manually fire the swipe() method… I think now that every injection of
$ionicSwipeCardDelegate was probably creating a new instance and that’s why I could not see the cards… I was using the example which has 2 controllers … seems to be adding unnecessary complexity.

I made a false assumption that the injected class would be a shared instance. It seems to not be the case!

So I am combining the controllers in to one, and reversing my changes to the plugin… hopefully that fixes things!

Unfortunately it did not, because $scope is not passed to a factory. Even calling the function while passing $scope does not fix it. Back to $rootScope I guess.

I sympathise; it’s frustrating to ask “how can I do this” only to be told “I wouldn’t do that if I were you”, especially when you’ve been trying to get it working for a while.

Still a bit hard to see what end result you’re trying to achieve. I’ll have a wander around that repository if I get some time, but you’ll probably get something your happy with before that.