我只是想知道为什么不能在未定义的数组上制作forEach.
码:
var arr = new Array(5); // [undefined x 5]
//ES5 forEach
arr.forEach(function(elem, index, array) {
console.log(index);
});
//underscore each
_.each(arr, function(elem, index, array) {
console.log(index);
});
Run Code Online (Sandbox Code Playgroud)
两个示例都不执行功能.
现在要做到foreach,我必须做:
var arr = [0,0,0,0,0];
Run Code Online (Sandbox Code Playgroud)
然后为它做每一个.
我试图创建一个具有指定大小的数组并循环它,避免for循环.我认为使用forEach比使用循环更清楚.对于长度为5的数组,这不是问题,但是对于更大的数组来说它会很难看.
为什么在循环未定义值数组时出现问题?
我有一个组件,我传递模板.在这个组件内部,我想传递上下文,以便我可以显示数据.
@Component({
selector: 'my-component',
providers: [],
template: `
<template [ngTemplateOutlet]="templ" [ngOutletContext]="{isVisible: true}">
</template>
`
})
export class MyElementComponent implements OnInit {
@ContentChild(TemplateRef) templ;
constructor(){}
}
Run Code Online (Sandbox Code Playgroud)
现在在其他组件中使用组件时:
<my-component>
<template>
{{isVisible ? 'yes!' : 'no'}}
</template>
</my-component>
Run Code Online (Sandbox Code Playgroud)
所以在my-component我传递一个模板,该模板在其类的@ContentChild名称中处理templ.
然后,在my-component传递templ给我的模板中ngTemplateOutlet,另外,我正在传递ngOutletContext已isVisible设置为的上下文true.
我们应该yes!在屏幕上看到,但似乎上下文永远不会通过.
我的角度版本:
"@angular/common": "^2.3.1",
"@angular/compiler": "^2.3.1",
"@angular/core": "^2.3.1",
Run Code Online (Sandbox Code Playgroud) javascript typescript angular2-directives angular2-template angular
我正在构建一个应用程序,通过ajax将文件上传到服务器.问题是用户很可能有时不会有互联网连接,并且客户希望在用户重新连接时安排ajax调用.这可能是用户在离线时安排文件上传并关闭应用程序.应用程序关闭时(不在后台)可以进行ajax调用吗?当应用程序在后台时,我们可以使用后台获取,但是当应用程序关闭时,我从未遇到过做某事的问题.
当通过npm安装任何东西时,它会下载许多不需要的文件.通常我正在寻找一个库最终版本,一个*.min.js文件或类似的东西,但其余的是没用的.
你如何处理所有这些无用的文件?您是手动删除它们还是使用任何构建工具生成最终应用程序,如gulp或grunt?
我很困惑,因为我的webapp中安装了大量的npm模块,文件夹大小约为50兆字节,但可能只有2mb.
在角度CLI中创建模块时,我们可以添加--routing-scope作为参数.
ng g m dashboard --routing-scope something-here --routing
Run Code Online (Sandbox Code Playgroud)
使用此命令时,我收到错误:
Schematic input does not validate against the
Schema: {"routingScope":"dashboard","routing":false,"spec":true,"flat":false,"commonModule":true}
Errors: Data path ".routingScope" should be equal to one of the allowed values.
Run Code Online (Sandbox Code Playgroud)
但是允许的值是多少?
文档中未描述此参数.
我正在创建可重用的表组件,该组件将允许编辑对象字段以准备将其发送到API。
有一个对象:
person: {
name: "John"
job: {
type: "IT"
title: "Software Engineer"
}
}
Run Code Online (Sandbox Code Playgroud)
我想将对象嵌套字段传递给组件并进行编辑。铁
<edit-field [field]="person.job.title"></edit-field>
Run Code Online (Sandbox Code Playgroud)
这将产生一个输入字段,该输入字段将完全编辑原始对象中的标题字段。问题在于person.job.title是一个字符串,而不是对象或数组,因此不会通过引用传递。
我有2个想法可以解决该问题:1)将“ person.job.title”作为字符串传递:
<edit-field [field]="'person.job.title'"></edit-field>
Run Code Online (Sandbox Code Playgroud)
要么
<edit-field field="person.job.title"></edit-field>
Run Code Online (Sandbox Code Playgroud)
并在组件类中用“。”分隔:
let fields = this.field.split('.');
Run Code Online (Sandbox Code Playgroud)
然后执行while循环以通过引用访问该字段。
我们还可以做2个输入:
<edit-field-component [fieldRef]="person.job" [field]="'title'"></edit-field-component>
Run Code Online (Sandbox Code Playgroud)
然后在组件内部做 this.fieldRef[this.field]
我想知道是否还有其他更干净的方法可以实现这一目标。
我使用了一些Session变量.当我注销时,函数show_number应该写0个数字,但它不是.
logout.php:
<?php
session_start();
session_destroy();
require_once('upper.php');
show_number(); //this function is declared on upper.php
?>
Run Code Online (Sandbox Code Playgroud)
index.php文件:
<?php
$_SESSION['var'] = 1;
echo "<a href="logout.php">Logout</a>
?>
Run Code Online (Sandbox Code Playgroud)
upper.php:
function show_number() { // shows value of $_SESSION['var'];
if (isset($_SESSION['var']))
echo "1";
else
echo "0";
}
Run Code Online (Sandbox Code Playgroud)
问题是:当我点击Logout链接时,echo仍会写入1号,我必须重新加载页面才能看到0值.
干杯
javascript ×4
angular ×3
angular-cli ×1
arrays ×1
cordova ×1
destroy ×1
foreach ×1
gruntjs ×1
gulp ×1
ios ×1
npm ×1
npm-install ×1
object ×1
objective-c ×1
php ×1
session ×1
swift ×1
typescript ×1
undefined ×1