I'm writing a small Angular app which relies on some common functions defined outside the app to do tasks across different controllers. In one of my controllers the scope object is declared when the controller is loaded. I'd like to be able to assign it later in one of my helper functions, but the assignment is not working.
Here's the relevant code:
.controller('myCtrl', function($scope, myFactory){
$scope.myObj;
$scope.saveEntry = function() {
myFactory.saveEntry($scope.obj);
formatSingleTableEntry($scope.obj, $scope.myObj);
$scope.obj = {};
}
});
// Entry Formatter
function formatSingleTableEntry(entry, scopeObject) {
if (typeof scopeObject === 'undefined') {
scopeObject = [];
}
if (typeof entry.obsDt === 'string') {
entry.obsDt = dateFormat(entry.obsDt);
}
scopeObject.push(entry);
}
// Date Formatter
function dateFormat(date) {
date = date.split('');
date[10] = 'T';
date = date.join('');
return Date.parse(date);
}
If I assign the scope object as an empty array in the controller, everything works, but assignment doesn't in this configuration. I can check to see that assignment actually happens in the helper function, but when I check in the controller it hasn't.
Why?
come backin more detail....each use is a new instance. If you need session persistence data needs to be stored in service$timeout()or$scope.$evalAsync()to start the digest process with the updates you did.