Im new to angular, to the answer for this question might be very straight forward, I just cant seem to find the solution.
I have an angular app with firebase as my backend, my general goal is to have vote card, I set up angular to sync with firebase, and populated firebase with the necessary data.
Each voting card has 4 options, each option paints the card in a different color, the votes are submitted to firebase and the data is consistent (if I refresh the page I can pull the votes from firebase).
What I cant do is to make angular click the button that was selected on page load (and thus painting the card on page load if the user voted on this card)
The angular code
var myApp = angular.module("MyApp", ["firebase"]);
function MyController($scope, $http, $firebase) {
var questionRef = new Firebase(<my.firebase.link>);
$scope.questions = $firebase(questionRef);
$scope.userId = "dave";
var answerRef = questionRef.child('responses').child($scope.userId);
var userRef = new Firebase(<another.firebase.link>);
$scope.selections = {};
$scope.newQuestion = {
options: []
};
//animating the cards to show vote options only on hover
$scope.showVotes = function(cardId){
AJS.$("#card"+cardId).animate({height:120}, 200);
}
$scope.hideVotes = function(cardId) {
AJS.$("#card"+cardId).animate({height:75}, 200);
}
//THE FUNCTION THAT PAINTS THE CARDS ACCORING TO SELECTION
/*$scope.paintCard = function(event) {
var card = AJS.$(event.target).parent().parent().parent();
card.removeClass();
card.addClass("card-cell "+AJS.$(event.target).data("card"));
}*/
$scope.$watch('selections', function () {
angular.forEach($scope.selections, function (value, key) {
userRef.child(key).set(true);
questionRef.child(key).child('responses').child($scope.userId).set(value);
});
}, true);
$scope.$watch('questions', function () {
var cleanQuestions = angular.fromJson(angular.toJson($scope.questions));
$scope.selections = {};
angular.forEach(cleanQuestions, function (question, key) {
if (!question.responses || !question.responses[$scope.userId]) return;
var response = question.responses[$scope.userId];
//sets up a default value on page load if exists, works with radio buttons but not with buttons
$scope.selections[key] = response;
});
}, true);
}
The HTML code:
<div ng-app="MyApp" ng-controller="MyController">
<div id="card{{questionKey}}" ng-class="cardColor" class="card-cell" ng-repeat="(questionKey, question) in questions" ng-mouseenter="showVotes(questionKey)" ng-mouseleave="hideVotes(questionKey)">
<div class="card">
<h4><span ng-bind="question.title"></span></h4>
<div class="description"><span ng-bind="question.desc"></span></div>
<div class="aui-buttons votes">
<button class="aui-button vote-{{option.text}}" ng-value="key" ng-click="cardColor='card-option.text'" ng-model="selections[questionKey]" ng-repeat="(key, option) in question.options">{{option.text}}</button>
</div>
</div>
</div>
</div>
Thanks for the help!