我正在使用Angular2开始我的项目,开发人员似乎推荐RXJS Observable而不是Promises.
我已经实现从服务器检索元素列表(史诗).但是我如何通过使用例如id来过滤元素?
以下代码是从我的应用程序中提取的,现在显示最终的工作解决方案.让我们希望它有所帮助.
@Injectable()
export class EpicService {
private url = CONFIG.SERVER + '/app/'; // URL to web API
constructor(private http:Http) {}
private extractData(res:Response) {
let body = res.json();
return body;
}
getEpics():Observable<Epic[]> {
return this.http.get(this.url + "getEpics")
.map(this.extractData)
.catch(this.handleError);
}
getEpic(id:string): Observable<Epic> {
return this.getEpics()
.map(epics => epics.filter(epic => epic.id === id)[0]);
}
}
export class EpicComponent {
errorMessage:string;
epics:Epic[];
epic:Epic;
constructor(
private requirementService:EpicService) {
}
getEpics() {
this.requirementService.getEpics()
.subscribe(
epics => this.epics = epics,
error => this.errorMessage …Run Code Online (Sandbox Code Playgroud) 我一直在使用ImmutableJS和Angular 2,因为它在变化检测中具有性能优势.看到这里.
但是,我不太确定,为什么Immutable默认使用Angular 2.当没有显式数组时,它如何知道如何迭代值并显示它们?它是否只是在toJS()每次访问集合的值时调用?它是否实现了Angular 2自动调用的某种方法?
如果是这样,有没有办法定义自己的集合,也实现了这个方法?
一个例子:
Component({
selector: 'list',
template: '<ul><li *ngFor="#item of items">{{ item.id }}</li></ul>',
directives: [CORE_DIRECTIVES]
})
export class SiteComponent {
items: Immutable.List<Item>;
}
Run Code Online (Sandbox Code Playgroud) 让我说我已经习惯用角度1开发客户端SPA,但现在我想改变Angular 2.
在进行变革时,谁会成为一些重要的范例?
以下是一些可能有助于确定答案的问题:
我收到以下错误:
EXCEPTION: Cannot find a differ supporting object '[object Object]' in [files | async in Images@1:9]
Run Code Online (Sandbox Code Playgroud)
这是模板的相关部分:
<img *ngFor="#file of files | async" [src]="file.path">
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
export class Images {
public files: any;
public currentPage: number = 0;
private _rawFiles: any;
constructor(public imagesData: ImagesData) {
this.imagesData = imagesData;
this._rawFiles = this.imagesData.getData()
.flatMap(data => Rx.Observable.fromArray(data.files));
this.nextPage();
}
nextPage() {
let imagesPerPage = 10;
this.currentPage += 1;
this.files = this._rawFiles
.skip((this.currentPage - 1) * imagesPerPage)
.take(imagesPerPage);
console.log("this.files:", this.files);
}
}
Run Code Online (Sandbox Code Playgroud)
将console.log在年底显示,它是一个可观察到的: …
我目前正在使用EitherT堆叠Futures和Eithers:
type ErrorOr[A] = Either[Error, A]
def getAge: Future[ErrorOr[Int]] = ???
def getDob(age: Int): ErrorOr[LocalDate] = ???
for {
age <- EitherT(getAge)
dob <- EitherT.fromEither[Future](getDob(age))
} yield dob
Run Code Online (Sandbox Code Playgroud)
我现在想介绍一下Writer monad ie
type MyWriter[A] = Writer[Vector[String], ErrorOr[A]]
def getAge: Future[MyWriter[Int]] = ???
def getDob(age: Int): MyWriter[LocalDate] = ???
Run Code Online (Sandbox Code Playgroud)
我的问题是,对调用getAge和getDob调用进行排序的最佳方法是什么?我知道monads可以堆叠,Future -> Writer -> Either但是我可以继续EitherT在这种情况下使用吗?如果是这样的话?
Scala不可变集合中的结构共享非常简单,并且有很多材料可以用来理解它.
现在每个Scala都case class自动定义一个copy方法,该方法返回一个带有指定新属性的新副本,我的问题是,该方法是否使用结构共享?
所以,当我有一个
case class A(x: HugeObject, y: Int)
Run Code Online (Sandbox Code Playgroud)
并调用该copy方法
val a = A(x,y)
val b = a.copy(y = 5)
Run Code Online (Sandbox Code Playgroud)
它复制x?
我有这样的枚举:
object Animals extends Enumeration {
type Animals = Value
val Monkey = Value("Monkey")
val Lion = Value("Lion")
val Dog = Value("Dog")
val Cat = Value("Cat")
}
Run Code Online (Sandbox Code Playgroud)
我需要从这个枚举中随机选择一个元素.我怎样才能在scala中有效地做到这一点?
在我的service.component.j中,我从http调用返回一个Student数组的observable.Student对象有一个名为age的属性,
student.ts
export class Student {
id: number;
name: string;
age:number;
}
Run Code Online (Sandbox Code Playgroud)
service.componnet.js
getStudents (): Observable< Student[]>
Run Code Online (Sandbox Code Playgroud)
在作为上述观察者的观察者的studentManagement.component.ts中,我想总结学生的年龄.我知道我可以将sum()放在源代码中(这是不太优选的,因为我还需要在页面中显示学生的其他信息,例如id,name,个别年龄.)或者从_studentList计算它.除了这两个,还有其他方式吗?
private _studentList:Student[]=[];
.subscribe(
returnedStudents => {
console.log(returnedStudents);
this._studentList = returnedStudents;
},
erroMsg => this._errorMessage = erroMsg
)
Run Code Online (Sandbox Code Playgroud) 我无法找到关于何时实例化Kotling单身人士的任何信息.我假设这是他们第一次被访问,但我无法在任何地方确认.
object Singleton{
val thing1 = 2
val thing2 = "Hello"
}
Run Code Online (Sandbox Code Playgroud)
该对象何时被实例化?首次访问房产时?当访问包装中的某些东西时?程序第一次运行时?
我正在使用该ng-disabled指令来控制何时可以单击页面上的按钮.是否可以通过计时器控制它?我希望按钮被移除直到5秒钟过去.我的HTML目前是:
<div ng-controller="ResetController">
<button ng-click="reset()">Reset</button>
</div>
Run Code Online (Sandbox Code Playgroud) angular ×5
javascript ×5
typescript ×4
rxjs ×3
scala ×3
angularjs ×2
observable ×2
clone ×1
copy ×1
enums ×1
immutability ×1
immutable.js ×1
kotlin ×1
monads ×1
random ×1
rxjs5 ×1
scala-cats ×1
singleton ×1