I am new to ionic and angular but have so far found it fun the try. I have a problem however. I have created a factory with a method and also some properties. If I update the property then the UI is not updating even though the $scope is set to the factory property in the controller. When the I run the app the value stays as false even though it is being set to true.
.controller('PlaylistsCtrl', function ($scope, $timeout, accountFactory) {
$scope.accounts = accountFactory.accountsProp;
$scope.valueLoaded = accountFactory.valueLoadedProp;
})
.factory('accountFactory', function () {
var accounts = [
{ id: 1, title: 'tetst1' },
{ id: 2, title: 'tetst2' },
{ id: 3, title: 'tetst3' }
],
accountsDetailed = [],
valueLoaded = false;
function populateAccountObjects() {
var sequence = Promise.resolve();
accounts.forEach(function (account) {
sequence = sequence.then(function () {
return account;
}).then(function (acc) {
valueLoaded = true;
console.log('loaded ' + acc.id);
});
});
}
var prom = new Promise(function (resolve, reject) {
window.setTimeout(function () {
populateAccountObjects();
resolve();
}, 5000);
});
return {
accountsProp: accounts,
accountsDetailedProp: accountsDetailed,
valueLoadedProp: valueLoaded
};
});
I have removed most of the code in the method and left the code above with least amount possible.
The outcome I am expecting is for the view to load with the list and the $scope.valueLoaded to be false in the UI (this is happening). But after 5 seconds the value is being set to ‘true’ and so I would expect the UI to change to true.
Can anyone spot what I have done wrong here?