app.factory('getLyric', function (httpq, $q, sync_lyric) {
var objlyric = {};
return {
getResponse: function (hurl) {
//return httpq.get(hurl).then(function (response) {
// objlyric = response.data;
// objlyric = sync_lyric.parseLyric(objlyric);
// return objlyric;
//});
var that = this,
request = new XMLHttpRequest();
//request.open('GET', hurl, true);
request.responseType = 'text';
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
text = request.response;
var data = {};
data = sync_lyric.parseLyric(text, data);
objlyric = data;
return objlyric;//tra ve duoc du lieu, nhung khong the dua ra controller
}
};
//request.onload = function () {
// text = request.response;
// var data = {};
// data = sync_lyric.parseLyric(text,data);
// return data;
//};
request.open("GET", hurl, true);
request.send();
return objlyric;
},
callbacklyric: function () { return objlyric; }
}
});
app.service('sync_lyric', function () {
var lyrics = {};
this.parseLyric = function (text, data) {
//get each line from the text
var lines = text.split('\n'),
//this regex mathes the time [00.12.78]
pattern = /[\d{2}:\d{2}.\d{2}]/g,
result = [];
//exclude the description parts or empty parts of the lyric
while (!pattern.test(lines[0])) {
lines = lines.slice(1);
};
//remove the last empty item
lines[lines.length - 1].length === 0 && lines.pop();
//display all content on the page
var tim = [];
var lyric = [];
//var lyrics = {};
lines.forEach(function (v, i, a) {
var time = v.match(pattern),
value = v.replace(pattern, '');
time.forEach(function (v1, i1, a1) {
//convert the [min:sec] to secs format then store into result
var t = v1.slice(1, -1).split(':');
//result.push([parseInt(t[0], 10) * 60000 + parseFloat(t[1])*1000, value]);
tim.push(parseInt(t[0], 10) * 60000 + parseFloat(t[1]) * 1000);
lyric.push(value);
});
});
lyrics["songTime"] = tim;
lyrics["songLyric"] = lyric;
//sort the result by time
//result.sort(function (a, b) {
// return a[0] - b[0];
//});
data = lyrics;
return lyrics;
};
});
Sau khi sửa đổi đôi chút, thì đã lấy đượcdữ liệu trả về từ service, tuy nhiên controller vẫn chưa lấy được dữ liệu? Vẫn dùng cách lấy dữ liệu của controller cũ?