I was just playing around with angular and its DI. I tried to use the promises paradigm as well, however I encountered a rather strange problem, using it in a Chrome Extension.
The following code works beautifully. The promise is resolved, when cb() in the object literal in the "chromeStorageService" is called.
But when I uncomment the return chrome.storage.local;
the code stops working. I can't understand why, because the console.log('going to be resolved');
still gets fired, but the success alert does not. Thanks in advance for any tip :)
https://developer.chrome.com/extensions/storage.html
'use strict';
var app = {};
app = angular.module('options', ['options.controllers']);
app.services = angular.module('options.services', []);
app.services.factory('chromeStorageService', function () {
//return chrome.storage.local;
return {
get: function(id, cb) {
cb();
}
}
});
app.services.factory('storageService', ['chromeStorageService', '$q', function (chromeStorage, $q) {
return new function () {
this.get = function (identifier) {
var defered = $q.defer();
chromeStorage.get(identifier, function (items) {
var error = false//chrome.runtime.lastError;
if (error) {
return defered.reject(error);
}
console.log('going to be resolved');
console.log(defered);
defered.resolve();
});
return defered.promise;
}
}
}]);
app.services.factory('fooService', ['storageService', function (testService) {
return new function () {
this.getAll = function () {
var promise = testService.get('foo');
console.log(promise);
promise.then(
function () {
alert('success');
}, function () {
aler('err');
}
);
}
}
}]);
app.directive('foo', ['fooService', function (fooService) {
return {
restrict:'A',
link:function (scope, elm, attrs, ctrl) {
fooService.getAll();
}
};
}]);
app.controllers = angular.module('options.controllers', ['options.services']);