我正在使用Laravel 5和Blade模板.在视图中,我想迭代一组Model对象,而不是一个数组数组.如果我确实想迭代一个数组数组,我会执行以下操作,它按预期工作:
$models = Foo::where('id', '>', 5)->get();
return view('home.index', ['models' => $models->toArray()]);
Run Code Online (Sandbox Code Playgroud)
但是,我想要一个具有可访问属性的对象数组.如果我要跑:
$models = Foo::where('id', '>', 5)->get();
return view('home.index', ['models' => $models->all()]);
Run Code Online (Sandbox Code Playgroud)
该var_dump是这样的:
object(Illuminate\Support\Collection)[164]
protected 'items' =>
array (size=3)
0 =>
object(App\Foo)[172]
public 'id' => null
public 'foo' => null
private 'created_at' => null
private 'updated_at' => null
protected 'connection' => null
protected 'table' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => …Run Code Online (Sandbox Code Playgroud) 我更新到 Angular 5.0.2,它需要 Typescript 2.4.2。我现在在尝试编译时收到此错误:
TypeError: Class extends value undefined is not a constructor or null
从我的研究来看,这似乎是一个循环依赖问题,但我不确定如何解决它。我有大量扩展基本模型的模型。例如(在伪代码中):
在 base-model.ts
export class BaseModel {
public name: string;
}
Run Code Online (Sandbox Code Playgroud)
在 foo-model.ts
import {BaseModel} from './base-model';
export class FooModel extends BaseModel {}
Run Code Online (Sandbox Code Playgroud)
在 bar-model.ts
import {BaseModel} from './base-model';
import {FooModel} from './foo-model';
export class BarModel extends BaseModel {
public foo: FooModel;
}
Run Code Online (Sandbox Code Playgroud)
在 index.ts
export * from './base-model';
export * from './foo-model';
export * from './bar-model';
Run Code Online (Sandbox Code Playgroud)
在 bar.component.ts
import {BarModel} from './index';
@Component({}) …Run Code Online (Sandbox Code Playgroud) 在RxJS,过滤器,如auditTime和throttleTime发射特定持续时间后可观察到的(以不同的方式)已经过去了.我需要发出一个Observable,然后在发出下一个值之前等待一段时间.
就我而言,我在Angular工作.例如,这段代码:
this.fooService$.pipe(throttleTime(10000)).subscribe(() => this.doSomething());
Run Code Online (Sandbox Code Playgroud)
不能完成我需要的东西,因为排放发生在持续时间结束时.我需要相反:发射然后延迟.我怎么能做到这一点?
使用Angular 2,我从服务接收JSON数据.像这样的东西:
{
"customerName": "foo",
"customerAddress": "123 Somewhere",
"products":
[
{
"productName": "bar",
"productNumber": 123
},
{
"productName": "baz",
"productNumber": 456
}
]
}
Run Code Online (Sandbox Code Playgroud)
在我的组件中,我订阅了服务以获取填充customerData.
private _getData() {
this._myService
.getData()
.subscribe(
customerData => this.customerData = customerData,
error => this.errorMessage = error
);
}
Run Code Online (Sandbox Code Playgroud)
此代码正常工作.
在JSON转储中,有两个数据"分组",即客户数据和产品数据,后者在数组中.理想情况下,我想分别填充产品数据.像这样的东西:
.subscribe(
customerData => this.customerData = customerData,
productData => this.productData = customerData.products
error => this.errorMessage = error
);
Run Code Online (Sandbox Code Playgroud)
这是可能的,如果是这样,我将如何编码订阅?
在Angular 5.1中使用DatePipe,我需要以小写形式格式化句点字段类型(AM/PM).根据文件,
Tuesday, December 19, 7:00 am
Run Code Online (Sandbox Code Playgroud)
应该
date:'EEEE, MMMM d, h:mm a'
Run Code Online (Sandbox Code Playgroud)
但是,句点字段类型始终以大写形式显示,所以我看到:
Tuesday, December 19, 7:00 AM
Run Code Online (Sandbox Code Playgroud)
我做错了什么,或者这是Angular日期格式化的已知缺陷?
在 Angular 组件中,我有一个值数组。在相关模板中,我想在两列中显示这些值。该模板使用Bootstrap框架。
由于 Angular 模板需要循环中的开始和结束标记,因此您无法通过打印</div><div class="row">来提供精确的换行符。将数组拆分为两列的正确方法是什么?
伪代码示例(这不起作用):
public foobars = ['a', 'b', 'c', 'd', 'e'];
----
<ng-container *ngFor="let foo of foobars; let i = index">
<ng-container *ngIf="i % 2 === 0; else secondColumn">
<div class="row">
<div class="col-md-6">{{foo}}</div>
</div>
</ng-container>
<ng-template #secondColumn>
<div class="col-md-6">{{foo}}</div>
</ng-template>
</ng-container>
Run Code Online (Sandbox Code Playgroud) 我需要确定处理 Observable 的两种不同方法是否同样有效,或者一种方法是否会导致内存问题。
在以下示例中,foo$和bar是从服务接收其值的模板变量。每个都有自己的 Observable。在组件中,bar从订阅中明确给出其值,然后在OnDestroy(). foo$但是,它没有显式订阅服务,而是async在模板中使用管道。
是foo$和bar都是显示服务数据的有效方式,还是foo$因为没有取消订阅内存清理而有问题?
示例服务:
Injectable()
export class ExampleService {
get foo$(): Observable<string> {
return data.from.api;
}
get bar$: Observable<string> {
return data.from.api;
}
}
Run Code Online (Sandbox Code Playgroud)
示例组件:
@Component({
template: `
<div>{{ foo$ | async }}</div>
<div>{{ bar }}</div>
`
})
export class ExampleComponent implements OnInit, OnDestroy {
public foo$ = this._exampleService.foo$;
public bar = '';
private _destroy$ = new Subject();
constructor(private …Run Code Online (Sandbox Code Playgroud) Angular Material 表利用 MatTableDataSource 方法来sortData()对表的数据源进行排序。(https://material.angular.io/components/table/api#MatTableDataSource)。它是一个箭头函数:
sortData: ((data: T[], sort: MatSort) => T[])
Run Code Online (Sandbox Code Playgroud)
我需要查看排序后的数据而不实际更改排序。例如,
this.dataSource.sortData = (sortData) => {
console.log(sortData);
return sortData;
};
Run Code Online (Sandbox Code Playgroud)
这里的问题是我重写了本机排序功能。return sortData返回原始数据,不进行任何排序。我想做的就是观察排序后的数据而不修改它。是否有可能做到这一点?
angular-material angular angular-material-table angular-material-7
我正在使用 Angular 9。我在根目录中有一个 JSON 文件,我不想将其包含在应用程序中。换句话说:
/src
|-- /app
|-- /assets
|-- index.html
|-- do_not_include_this_file.json
Run Code Online (Sandbox Code Playgroud)
这意味着http://example.com/do_not_include_this_file.json应该被 RouterModule 忽略,而是直接转到文件。目前该路径将重定向到 PageNotFoundComponent,例如
/src
|-- /app
|-- /assets
|-- index.html
|-- do_not_include_this_file.json
Run Code Online (Sandbox Code Playgroud)
如何让 RouterModule 完全忽略路径?
使用 Angular v4.4.4,我使用元素(submit)上的事件保存表单<form>。在实时代码上,一切正常。但是,在单元测试中单击 a<button>不会触发(submit)并且测试失败。例如,
组件(伪代码):
@Component({
template: `
<form [formGroup]="formGroup" (submit)="onSave()">
Your name: <input type="text" formControlName="name">
<button id="saveButton">Save</button>
</form>
`
})
export class FooComponent {
public formGroup: FormGroup;
public onSave(): void {
// save and route somewhere
}
}
Run Code Online (Sandbox Code Playgroud)
单元测试(伪代码):
describe('FooComponent', () => {
let fixture, component, _router, routerSpy;
beforeAll(done => (async() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([]),
FormsModule,
ReactiveFormsModule
]
});
fixture = TestBed.createComponent(FooComponent);
component = fixture.componentInstance;
_router = fixture.debugElement.injector.get(Router);
routerSpy …Run Code Online (Sandbox Code Playgroud) angular ×8
angular-pipe ×2
angular5 ×2
rxjs ×2
iterator ×1
laravel ×1
laravel-5 ×1
php ×1
rxjs6 ×1
unit-testing ×1