我的服务是:
myApp.service('userService', [
'$http', '$q', '$rootScope', '$location', function($http, $q, $rootScope, $location) {
var deferred;
deferred = $q.defer();
this.initialized = deferred.promise;
this.user = {
access: false
};
this.isAuthenticated = function() {
this.user = {
first_name: 'First',
last_name: 'Last',
email: 'email@address.com',
access: 'institution'
};
return deferred.resolve();
};
}
]);
Run Code Online (Sandbox Code Playgroud)
我config通过以下方式在我的文件中调用它:
myApp.run([
'$rootScope', 'userService', function($rootScope, userService) {
return userService.isAuthenticated().then(function(response) {
if (response.data.user) {
return $rootScope.$broadcast('login', response.data);
} else {
return userService.logout();
}
});
}
]);
Run Code Online (Sandbox Code Playgroud)
但是,它抱怨说这then不是一个功能.我不回复已解决的承诺吗?
我创建了一个启动服务的应用程序,它启动一个计时器,在一段时间后触发一个小函数.这在模拟器和Motorola Droid 1上运行得很好,但是当手机进入睡眠模式时,它在Droid X上不起作用.我发现的是,当手机处于睡眠状态时,计时器似乎会暂停.它似乎没有在Droid 1或模拟器上执行此操作.我确定解决方法并不太难,所以我不是要求帮助(一次)我只想要一个解释,以便更好地理解这一点.
我的问题是什么"睡眠模式" 在Android系统上做了什么?什么是停止,什么不停止,等等.睡眠模式我的意思是,当你按下电源按钮,屏幕变黑.究竟发生了什么?任何见解都表示赞赏.
请原谅我的无知,但我刚刚使用npm安装jQuery,在jQuery文件之间有一个名为的文件jquery.slim.js,是什么slim?我知道min缩小的立场但是苗条对我来说是新的!
顺便说一句我很确定slim不是min因为有另一个文件被调用jquery.slim.min.js,显然它比普通的超薄文件轻.
该slim文件还包含jQuery 3 beta.(这就是代码中的评论所说的)
再次抱歉这个愚蠢的问题,但我不知道.
UPDATE
我正在使用require包含文件,在我不知情的情况下需要包含此文件吗?或不?
似乎有一些问题将async/await与.reduce()结合起来,如下所示:
const data = await bodies.reduce(async(accum, current, index) => {
const methodName = methods[index]
const method = this[methodName]
if (methodName == 'foo') {
current.cover = await this.store(current.cover, id)
console.log(current)
return {
...accum,
...current
}
}
return {
...accum,
...method(current.data)
}
}, {})
console.log(data)
Run Code Online (Sandbox Code Playgroud)
该data对象被记录之前的this.store完成...
我知道你可以使用Promise.all异步循环,但这适用于.reduce()?
来自jquery api页面 http://api.jquery.com/jQuery.getScript/
成功回调
一旦脚本加载但未必执行,则会触发回调.
Run Code Online (Sandbox Code Playgroud)$.getScript("test.js", function() { foo(); });
如果foo()函数依赖于test.js,则无法成功执行.
我在google map api中看到了类似的东西,但在这种情况下你可以在ajax脚本url中指定回调函数.但总的来说有一种明显的方法可以等到执行ajax脚本来执行回调吗?
我总是这样成功地对我的数组进行排序(当我不想要标准的词典排序时):
var arr = […] // some numbers or so
arr.sort(function(a, b) {
return a > b;
});
Run Code Online (Sandbox Code Playgroud)
现在,有人告诉我这是错的,我需要return a-b改为.这是真的,如果是的话为什么?我测试了我的比较功能,它有效!另外,为什么我的解决方案在出错时会如此常见?
当async/await在node.js函数中使用时,它会阻塞node.js线程,直到它执行下一行代码吗?
我正在从JSON事件源解析日期 - 但日期在IE7/8中显示"NaN":
// Variable from JSON feed (using JQuery's $.getJSON)
var start_time = '2012-06-24T17:00:00-07:00';
// How I'm currently extracting the Month & Day
var d = new Date(start_time);
var month = d.getMonth();
var day = d.getDate();
document.write(month+'/'+day);// "6/24" in most browsers, "Nan/Nan" in IE7/8
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?谢谢!
我一直在玩 JS,但无法弄清楚 JS 如何决定在使用Array.from(). 例如,以下表情符号的 alength为 2,因为它由两个代码点组成,但是,Array.from()将这两个代码点视为一个,给出一个包含一个元素的数组:
const emoji = '';
console.log(Array.from(emoji)); // Output: [""]Run Code Online (Sandbox Code Playgroud)
但是,其他一些字符也有两个代码点,例如这个字符??(也有一个.length2)。但是,Array.from不会“分组”此字符,而是生成两个元素:
const str = '??';
console.log(Array.from(str)); // Output: ["?", "?"]Run Code Online (Sandbox Code Playgroud)
我的问题是:当字符由两个代码点组成时,是什么决定了字符是被分解(如示例二)还是被视为一个单一元素(如示例一)?
我正在玩React Native.我正在尝试构建我的应用程序,但它开始变得混乱进口.
--app/
-- /components
-- Loading.js
-- index.ios.js
Run Code Online (Sandbox Code Playgroud)
现在,index.ios.js我可以简单地做到:
import Loading from './components/Loading';
但是,当我开始创建更多组件时,使用更深的目录结构,它开始变得混乱:
import Loading from '.../../../../components/Loading';
我理解首选的解决方案是为事物制作私有的npm模块,但这对于一个小项目来说太过分了.
您可以global.requireRoot在浏览器上执行类型解决方案,但如何通过导入实现此功能?