I'm trying to reset a $scope variable in my controller when a url parameter changes, but I'm having some trouble with it holding on to old values.
I have a site I'm building for a law firm and if I click to one of the attorney's bios from any page except the bio page itself, it works fine. But, if I try to click to a new attorney's bio page when I'm already on the bio page, it doesn't seem to reset my $scope.thisAttorney variable but rather creates a second instance of it.
The problem with this is that I have a box with rotating quotes about the current attorney that's set up with a timeout function. So, when this problem hits, it has two sets of quotes rotating in that box. I need it to forget about the first attorney when I click on the second attorney's bio.
Here are what I think are relevant files. Please just ask if you need to see something else.
app.js
var app = angular.module("LKSU", ['ngRoute']);
app.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl: 'content.php',
controller: 'HomeController'
})
.when('/bios/:user_id?', {
controller: 'AttorneyController',
templateUrl: 'bio.php'
})
.otherwise({
redirectTo: '/'
});
});
AttorneyController.js
app.controller('AttorneyController', ['$scope', '$location', 'attorneys', '$sce', '$routeParams', function($scope, $location, attorneys, $sce, $routeParams) {
$scope.myFunctions = {};
var practiceareas = {
altdispute: "Alternative Dispute Resolution",
businesscorp: "Businesses & Corporations",
estateplanning: "Estate Planning",
futures: "Futures & Derivatives",
litigation: "Litigation",
productliability: "Product Liability",
realestate: "Real Estate",
securities: "Securities"
};
function quoteflip(quotelist, id, total){
clearTimeout(timeoutQuotes);
alert($scope.thisAttorney.name + " 1"); // This is how I know it's holding onto the first attorney in $scope.thisAttorney
var idno = (id + 1) % total;
$("#bio_quotes").html(quotelist[id]);
var src1 = quotelist[idno];
$("#bio_quotes").fadeOut(500, function(){
$("#bio_quotes").html(src1).fadeIn(500);
});
timeoutQuotes = window.setTimeout(function(){quoteflip(quotelist, idno, quotelist.length);}, 5000);
}
var timeoutQuotes = "";
attorneys.success(function(data){
if($routeParams.user_id > 0){
var matches = $.grep(data.attorneys, function(obj) { return obj.id == $routeParams.user_id; });
if (matches.length === 1) {
$scope.thisAttorney = "";
$scope.thisAttorney = matches[0];
$scope.$apply();
var src = $scope.thisAttorney.quotes[0];
$("#bio_quotes").html(src).fadeIn(500);
clearTimeout(timeoutQuotes);
$scope.attorneys = data.attorneys;
$scope.practiceareas = practiceareas;
timeoutQuotes = window.setTimeout(function(){quoteflip($scope.thisAttorney.quotes, 0, $scope.thisAttorney.quotes.length);}, 5000);
}
}else{
$scope.myFunctions.bio_id = 0;
};
});
}]);
Thoughts?
For the record, I tried to put quoteflip in the main script.js but timeout call couldn't find it so I had to bring it back into the Controller. If anyone has a fix for that, i.e.: sees my problem, please feel free to comment on that as well. Thanks.