使用$ q获取一些奇怪的行为。它自称无限并冻结我的PC。不知道出了什么问题......也许是因为我正在解决img onload事件?这是工厂和控制器的代码
function setImgSize(picFile) {
var deferred = $q.defer();
var _URL = window.URL || window.webkitURL;
var imgDOMElement = angular.element(document.getElementsByClassName('test-dialog')[0])
var compElement = window.getComputedStyle(imgDOMElement[0], null);
var width = compElement.getPropertyValue("width");
var height = compElement.getPropertyValue("height");
var img = new Image();
img.src = _URL.createObjectURL(picFile);
img.onload = function() {
if (img.width >= (+width.slice(0, -2) - 50)) {
deferred.resolve("true message placeholder");
} else {
deferred.reject("false message placeholder");
}
console.log(img.width)
}
return deferred.promise;
}
return {
setImgSize: setImgSize
}
控制器是另一个控制器内的嵌套DialogController。我正在构建Angular材料应用程序......
$scope.setImagePlaceholder = function(picFile) {
var file = picFile.type;
DocumentFactory.setImgSize(picFile).then(function(res) {
console.log("response is : " + res);
$scope.resizeImg = true;
}, function(err) {
console.log(err);
});
if (/image/gi.test(file)) {
return picFile;
}
return 'https://scontent-fra3-1.xx.fbcdn.net/v/t1.0-9/13427833_10156897153540018_1661210034952098241_n.jpg?oh=6124bc10ca57d3b46c845494dd7c0833&oe=57E71A2B';
}