我需要能够在运行时合并两个(非常简单的)JavaScript对象.例如,我想:
var obj1 = { food: 'pizza', car: 'ford' }
var obj2 = { animal: 'dog' }
obj1.merge(obj2);
//obj1 now has three properties: food, car, and animal
Run Code Online (Sandbox Code Playgroud)
有没有人有这个脚本或知道内置的方法来做到这一点?我不需要递归,我不需要合并函数,只需要平面对象上的方法.
我们正在使用Angular 2和TypeScript构建应用程序.我们尝试静态检查可能的类型.有没有办法检查模板中的类型?请考虑以下片段:
<foo [data]="dataObj"></foo>
Run Code Online (Sandbox Code Playgroud)
假设data在Foo组件中有某种类型TData.但是,默认情况下,没有什么能阻止我传递dataObj不符合的内容TData.是否有Angular模板的typescript扩展,可以在这种情况下验证类型?
我正在尝试将我的angular2自定义库之一迁移到RC.6 + Webpack.我的目录结构是:
- src - source TS files
- lib - transpiled JS files + definition files
- dev - development app to test if it works / looks ok.
- myCustomLib.js - barrel
- myCustomLib.d.ts
Run Code Online (Sandbox Code Playgroud)
在dev文件夹内尝试运行应用程序.我引导我的模块:
app.module.ts
import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { AppComponent } from "./app.component";
import { MyCustomModule } from "../../myCustomLib";
@NgModule({
imports: [
BrowserModule,
MyCustomModule
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
}) …Run Code Online (Sandbox Code Playgroud) 有没有办法在ngModelChange上获取字段的上一个(最后一个)值?我的东西是这样的
HTML
<input type="text" [(ngModel)]="text" (ngModelChange)="textChanged($event)">
Run Code Online (Sandbox Code Playgroud)
处理器
private textChanged(event) {
console.log('changed', this.text, event);
}
Run Code Online (Sandbox Code Playgroud)
我得到的是
changed *newvalue* *newvalue*
Run Code Online (Sandbox Code Playgroud)
当然我可以使用另一个变量保留旧值,但有更好的方法吗?
我在Angular 4中创建了一个表单,允许用户单击表单中的ADD或REMOVE按钮向表单添加/删除字段.我使用ngFor从一个数组创建屏幕上的html输入(由add函数放大,或者由remove函数缩小).
在html模板中,我可以以formControlName ="control {{index}}"的形式添加formControlName,以确保每个新输入都有一个formcontrol.
但是,如何为这些输入动态添加和删除验证器?
我显示的秒数从3600减少到0.
<div class="time-box" *ngIf="p.value.playerTimer > 0">{{ p.value.playerTimer }}</div>
Run Code Online (Sandbox Code Playgroud)
我想将秒数格式化为分钟:秒
我希望能够使用DatePipe - https://angular.io/api/common/DatePipe
<div class="time-box" *ngIf="p.value.playerTimer > 0">{{ p.value.playerTimer | date:'mmss' }}</div>
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为它预计p.value.playerTimer是一个日期对象或自纪元以来的秒数.
还有另一种方法来实现这一目标吗?
我有以下拦截器:
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(private tokenService: TokenService) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const token = this.tokenService.getToken();
if (token) {
const authReq = req.clone({
headers: req.headers.set('Authorization', `Bearer ${token}`)
});
return next.handle(authReq);
}
return next
.handle(req)
//
.getSomehowTheResponse()
.andSaveTheTokenInStorage()
.andPropagateNextTheResponse()
}
}
Run Code Online (Sandbox Code Playgroud)
我想从本地存储中的响应头保存令牌,所有教程都显示如何拦截请求,但不是很清楚响应.
我想从第2页开始点击事件.第1页有第2页的链接,点击按钮时有一个按钮,它在第1页上添加了一行HTML.我正在尝试用于传递数据.我的代码不起作用,请看下面:localStorage
1st Page.html
HTML
<div id="content">
</div>
Run Code Online (Sandbox Code Playgroud)
JS
var output = document.getElementById('content');
addEvent(window, 'storage', function (event) {
if (event.key == 'StorageName') {
output.innerHTML = event.newValue;
}
});
Run Code Online (Sandbox Code Playgroud)
第2页
HTML
<input id="data" type="button" value="+" onclick="addRow()">
Run Code Online (Sandbox Code Playgroud)
JS
addEvent(dataInput, 'keyup', function () {
localStorage.setItem('StorageName', this.value);
});
var dataInput = dataInput = document.getElementById('data');
object.onclick = function(){
addRow() {
var div = document.createElement('div');
div.className = 'row';
div.innerHTML = …Run Code Online (Sandbox Code Playgroud) 我有一个包含默认隐藏的 textarea 的组件:
<div class="action ui-g-2" (click)="toggleEditable()">edit</div>
<textarea [hidden]="!whyModel.inEdition" #myname id="textBox_{{whyModel.id}}" pInputTextarea focus="true" [(ngModel)]="whyModel.description"></textarea>
Run Code Online (Sandbox Code Playgroud)
当我单击“编辑”div 时,我想显示 textarea 并将焦点放在它上面:
@ViewChild('myname') input: ElementRef;
...
private toggleEditable(): void {
this.whyModel.toggleEditable();
this.input.nativeElement.focus();
}
Run Code Online (Sandbox Code Playgroud)
“显示”部分正在工作,但不是重点部分。我想念什么?
我正在研究一个Angular 2项目.我使用ngx-datatable显示用户列表:
<ngx-datatable class="material"
[rows]="rows"
[columnMode]="'force'"
[headerHeight]="40"
[footerHeight]="40"
[rowHeight]="30"
[externalPaging]="true"
[limit]="limit"
(select)='onSelect($event)'
(activate)='onActivate($event)'
[selected]="selected"
[selectionType]="'checkbox'">
<ngx-datatable-column
[width]="30"
[sortable]="false"
[canAutoResize]="false"
[draggable]="false"
[resizeable]="false"
[headerCheckboxable]="true"
[checkboxable]="true">
</ngx-datatable-column>
<ngx-datatable-column name="Name">
<ng-template let-value="value" ngx-datatable-cell-template>
<div class="redNumber">{{value}}</div>
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="Title"></ngx-datatable-column>
<ngx-datatable-column name="company"></ngx-datatable-column>
<ngx-datatable-column name="Status" [cellClass]="getStatusClass">
</ngx-datatable-column>
<ngx-datatable-column name="Last connexion"></ngx-datatable-column>
Run Code Online (Sandbox Code Playgroud)
当我点击触发以下SHOWALL()方法,什么也不会发生,除非我点击第二次按钮,列表被刷新,或者如果我点击一次,调整我的浏览器窗口,列表被刷新.如何能我正确刷新列表?我究竟做错了什么?在我的组件类中,我有这个:
export class M1OamRoomsListComponent{
selected = [];
public rows = [
.... // list of users
];
public limit: number;
constructor(public translation: TranslationService){
this.limit = 10;
}
getStatusClass({ row, column, value }): any {
return { …Run Code Online (Sandbox Code Playgroud)