在Angular 2文档页面上有一个使用Http服务的例子.
getHeroes (): Observable<Stuff[]> {
return this.http.get(this.url)
.map(this.extractData)
.catch(this.handleError);
}
Run Code Online (Sandbox Code Playgroud)
我克隆了angular2-webpack-starter项目并自己添加了上面的代码.
我导入Observable使用
import {Observable} from 'rxjs/Observable';
Run Code Online (Sandbox Code Playgroud)
我假设属性Observable也被导入(.map有效).查看rxjs.beta-6的更改日志,没有提到任何内容catch.
这与Angular 2官方发布有关.我知道单元测试在beta,RC和官方发布之间发生了巨大变化.
当它在构造函数中用作参数时,在单元测试中模拟@ ngrx/store的好方法是什么?它并不像模拟服务那么简单.
例如,如果我想模拟服务,那么我可以这样做:
let serviceStub = { }; // not a true mocked service, just a stub, right?
let de: DebugElement;
let el: HTMLElement;
let nativeEl: Element;
let comp: Typeahead;
let fixture: ComponentFixture<Typeahead>;
describe('Component:Typeahead', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [...],
declarations: [Typeahead],
providers: [
{provide: TypeaheadService, useValue: serviceStub} // provides the service that is being "mocked"
]
}).compileComponents();
fixture = TestBed.createComponent(Typeahead);
nativeEl = fixture.nativeElement;
comp = fixture.componentInstance;
de = fixture.debugElement;
});
});
Run Code Online (Sandbox Code Playgroud)
这很有效.
对于ngrx/store …
我只想试着了解AngularJS的基础知识.我尝试编写一个简单的MVC应用程序,但控制器似乎没有工作.该文件可以找到刚找到的有角度的lib,我已经通过排除'ng-controller'进行了测试.
<!DOCTYPE html>
<html ng-app>
<head>
<script src='js/lib/angular.js'></script>
<script>
// controller
function MyFirstCtrl($scope) {
// model
var employees = ['Christopher Grant', 'Monica Grant', 'Christopher
Grant', 'Jennifer Grant'];
$scope.ourEmployees = employees;
}
</script>
</head>
<body ng-controller='MyFirstCtrl'>
<!-- view -->
<h2>Number of employees: {{ourEmployees.length}}</h2>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
编辑:错误日志说明如下:
Uncaught SyntaxError: Unexpected token ILLEGAL , line 9
Uncaught SyntaxError: Unexpected token { , line 19
Error: [ng:areq] Argument 'MyFirstCtrl' is not a function, got undefined
Run Code Online (Sandbox Code Playgroud)
EDIT2:我把代码更改为:
<!DOCTYPE html>
<html ng-app='MVCExample'>
<head>
<script src='js/lib/angular.js'></script>
<script> …Run Code Online (Sandbox Code Playgroud) 我只是想从下拉菜单中绑定数据ngModel。当应用程序加载时,我收到了一个有意义的错误。
browser_adapter.js:84 EXCEPTION: No value accessor for ''
Run Code Online (Sandbox Code Playgroud)
这使我相信该错误源于ngModel应用程序加载时最初未绑定任何数据的事实。
我不是最擅长使用 Observables...所以要小心。
部分 html 下拉菜单
<p-dropdown [options]="actionsToTake" (onChange)="onToggleCreateActionInput()"
[(ngModel)]="action"></p-dropdown>
Run Code Online (Sandbox Code Playgroud)
相关的 TypeScript(排除进口)
export class ActionView {
public actionsToTake: SelectItem[] = [];
public action: Action = new Action();
constructor (private actionCreateService: ActionCreateService) {
// populate dropdown list (actionsToTake) with data from service call
this.actionCreateService.getActionFields().subscribe((resp) => {
for (let i = 0; i < resp.data.data.actionElm.length; i++) {
this.actionsToTake.push({label: resp.data.data.actionElm[i].name,
value: resp.data.data.actionElm[i].name});
}
});
}
public onToggleCreateActionInput = (action): void => …Run Code Online (Sandbox Code Playgroud) angular ×3
typescript ×2
angularjs ×1
jasmine ×1
javascript ×1
ngrx ×1
primeng ×1
rxjs ×1
unit-testing ×1