我决定使用Observable而不是Http承诺.
这就是我的Promise服务的样子:
export class MovieService {
movies: Movie[]
movie: Movie;
constructor(private http:Http) { }
getMovies(): Promise<Movie[]>{
return this.http.get('http://api.request.com')
.toPromise()
.then((res:Response) => res.json()['results'])
}
getMovie(id: number): Promise<Movie> {
return this.getMovies()
.then(movies => movies.find(movie => movie.id == id));
}
}
Run Code Online (Sandbox Code Playgroud)
首先我获取一组电影,而不是通过id找到某个数组的电影.但是,当我尝试对Observable执行相同操作时,我收到有关find的错误通知:属性'find'在类型'Movie []'上不存在.
以下是我尝试使用Observable服务的内容:
export class MovieService {
movies: Movie[];
movie: Movie;
constructor(private http: Http) {
}
getMovies(): Observable<Movie[]> {
return this.http.get('http://api.request.com)
.map((res: Response) => res.json()['results']);
}
getMovie(id: number): Observable<Movie> {
return this.getMovies()
.subscribe(movies => movies.find(movie => movie.id == id));
}
}
Run Code Online (Sandbox Code Playgroud)
如何在我的Observable服务中实现相同的功能,就像在我的Promise服务中一样?
我有一个API请求,返回以下内容:
{"page": 1,
"results": [
{
"poster_path": "/9O7gLzmreU0nGkIB6K3BsJbzvNv.jpg",
"adult": false,
"overview": "Framed in the 1940s for the double murder of his wife and her lover, upstanding banker Andy Dufresne begins a new life at the Shawshank prison, where he puts his accounting skills to work for an amoral warden. During his long stretch in prison, Dufresne comes to be admired by the other inmates -- including an older prisoner named Red -- for his integrity and unquenchable sense of hope.", …Run Code Online (Sandbox Code Playgroud) 我想用绑定创建一个电影海报图像的网格视图.
我的viewmodel看起来像这样:
public class PopularMoviesViewModel extends BaseObservable {
Movie movie;
Context context;
MovieServiceComponent movieServiceComponent = DaggerMovieServiceComponent.builder()
.contextModule(new ContextModule(context))
.build();
Picasso getPicasso = movieServiceComponent.getPicasso();
public PopularMoviesViewModel(Movie movie, Context context) {
this.movie = movie;
this.context = context;
}
@Bindable
public String getImageUrl(){
return movie.posterPath();
}
@Bindable
public String getTitle(){
return movie.originalTitle();
}
@BindingAdapter({"imageUrl"})
public void setImageUrl(ImageView view, String poserPath){
getPicasso.with(view.getContext()).load("http://image.tmdb.org/t/p/w185"+ poserPath).into(view);
}
}
Run Code Online (Sandbox Code Playgroud)
布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<data class="PopularMoviesBinding">
<variable
name="pmvm"
type="com.hartyandi.oviesm.modelviews.PopularMoviesViewModel"></variable>
</data>
<LinearLayout
android:id="@+id/row"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:paddingBottom="0dp" …Run Code Online (Sandbox Code Playgroud) 我有 XML 指定以下内容:
<xs:element name="getNewsResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="newsItem" type="tns:newsList"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="newsList">
<xs:list itemType="tns:news"/>
</xs:simpleType>
<xs:complexType name="news">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="date" type="xs:string"/>
<xs:element name="author" type="tns:author"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="shortDescription" type="xs:string"/>
<xs:element name="content" type="xs:string"/>
</xs:sequence>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
我想在我的回复中列出新闻列表。但是,当我想用 jaxb2 创建 Java 对象时,当我运行 mvn clean compile -X 时,xml 返回以下错误:
org.xml.sax.SAXParseException: cos-st-restricts.1.1: The type 'newsList' is atomic, so its {base type definition}, 'tns:news', must be an atomic simple type definition or a built-in primitive datatype.
我应该如何更改我的 XML 以便能够编译?
我正在制作一个自定义验证器来检查输入是否是有效的电子邮件格式。
这是我的验证器类:
import {FormControl} from "@angular/forms";
export class EmailValidator {
static getEmailValidator() {
return function emailValidator(control: FormControl): { [s: string]: boolean } {
if (!control.value.match(/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/)) {
return {invalidChars: true};
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的 HTML 看起来像这样:
<div class="form-group">
<input class="form-control"
[(ngModel)]="model.email"
type="text"
id="name"
placeholder="Enter your email"
[formControl]="signupForm.controls['email']"
>
<div *ngIf="signupForm.controls['email'].errors?.required&& signupForm.controls['email'].touched"
class="alert alert-danger">Email is required</div>
<div *ngIf="signupForm.controls['email'].hasError('invalidChars') && signupForm.controls['email'].touched"
class="alert alert-danger">Please give a valid email address</div>
Run Code Online (Sandbox Code Playgroud)
我的 HTML 组件:
export class SignupComponent {
signupForm: FormGroup;
email: AbstractControl;
model: any = …Run Code Online (Sandbox Code Playgroud)