小编dev*_*033的帖子

无法使用Angular 2 rc5发出http请求

我刚刚升级到Angular 2 RC 5了一个我无法弄清楚的原因,我无法发出任何http请求(即请求永远不会出现/不会显示在浏览器日志中).

这是直到今天升级的代码.

console.log("Let's call github");
let url = 'https://raw.githubusercontent.com/angular/angular/master/LICENSE';
return this.get(url).do(d=>console.log('success'));
Run Code Online (Sandbox Code Playgroud)

当我的组件调用包含此代码的函数时,Let's call github会输出,但是不会发出请求.我确信这个ObservableHttp来自@angular/http正确导入,因为今天早上同样的代码工作.我的第一个想法是我错过了HttpModule,但我已经确认我将它导入我的主要版本AppModule

事实上,如果我注释掉HttpModule导入,我看不出任何区别.错误是一样的,如下:

Exception: TypeError: this._body is null
Run Code Online (Sandbox Code Playgroud)

这是堆栈跟踪的摘录(在底部是调用上面引用的函数的行): 在此输入图像描述

xmlhttprequest angular

6
推荐指数
1
解决办法
1014
查看次数

Angular2中的复选框组处理和验证

我不明白,应该如何在angular2中以模型驱动的形式处理复选框组.

该模型有一个languages我实例化的属性,如下所示:

this.model = {
  languages: []
};
Run Code Online (Sandbox Code Playgroud)

使用FormBuilder创建的形式:

this.modelForm = this.formBuilder.group({
  'languages': [model.languages, Validators.required],
});
Run Code Online (Sandbox Code Playgroud)

和模板:

<div *ngFor="let language of translateService.get('languages') | async | key_value">
  <input type="checkbox" name="languages[]" formControlName="languages" value="{{ language.key }}" id="language_{{ language.key }}">
  <label attr.for="language_{{ language.key }}">{{ language.value }}</label>
</div>
Run Code Online (Sandbox Code Playgroud)

div需要对造型的目的(定制复选框),key_value使按键和循环使用的语言,明显的价值.

第一个问题来自验证.如果检查并取消选中一个复选框(并且未选中其他复选框),则输入仍然有效 - 不知何故很奇怪.

第二个问题伴随着价值,即true如果检查两种或更多种语言,则false另外.

然后有第三个问题,其初始值languages只是一个空数组,但会导致所有复选框最初被检查(如果初始值设置为a string,则不会发生),尽管我无法发现任何checked属性DOM.

我正在使用最新的离子beta(2.0.0-beta.10),它使用角度/核心版本2.0.0-rc.4和角度/形式版本0.2.0.

那么,有没有关于如何使用复选框组的指南?有什么建议或想法吗?

ionic-framework ionic2 angular2-forms angular

6
推荐指数
1
解决办法
5182
查看次数

如何在 Angular 模板中获取表单控件验证错误

我有一个表单,它根据下拉列表中的某些值更改其验证。如果选择 A 大学,那么最低百分比要求为 60,如果选择 B 大学,则最低百分比会下降到 55。尽管我已经找到了一种方法来获取模板中的最小错误,这样我就不必费力了在我的模板中编写百分比值。虽然我不确定这是否是正确的方法。

<div formGroupName="marks">
  <div class="form-group">
    <label for="email" class="col-lg-3 control-label">Semester 1</label>
    <div class="col-lg-3">
      <input autocomplete="off"
             #sem1
             type="text"
             min="0"
             max="99"
             maxlength="1"
             maxlength="2"
             class="form-control"
             id="email"
             placeholder="Sem 1 (%)"
             (keypress)="onlyNumberKey($event)"
             formControlName="sem1">
      <span *ngIf="registrationForm.controls['marks'].controls['sem1'].hasError('required') &&
                   registrationForm.controls['marks'].controls['sem1'].touched"
                   class="text-danger">
        Sem 1 percentage required
      </span>
      <span *ngIf="registrationForm.controls['marks'].controls['sem1'].hasError('min') ||
                   registrationForm.controls['marks'].controls['sem1'].hasError('max') ||
                   registrationForm.controls['marks'].controls['sem1'].hasError('minlength')||
                   registrationForm.controls['marks'].controls['sem1'].hasError('maxlength') &&
                   registrationForm.controls['marks'].controls['sem1'].touched"
                   class="text-danger">
        Percenatge must be {{ registrationForm.get('marks.sem1')._errors.min.min }}  and above.
      </span>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

成分

registrationForm = new FormGroup({
  college: new FormControl('', [
    Validators.required, dropDrownValidator
  ]),
  marks: new FormGroup({ …
Run Code Online (Sandbox Code Playgroud)

angular2-forms angular angular-reactive-forms

6
推荐指数
1
解决办法
2万
查看次数

dir-pagination指令angularjs:有什么办法可以在分页中获取当前页面的所有记录吗?

我正在使用@michaelbromley的dir-pagination指令.我想获得当前指令页面上的所有记录.有没有办法做到这一点?

dir-paginate pic

这是链接:dir-pagination,所以我需要records从100到96 的5的集合.有没有快速的方法来做到这一点?

我尝试了几件但没有工作.

pagination angularjs angularjs-ng-repeat dirpagination

5
推荐指数
1
解决办法
2万
查看次数

Safari浏览器上的属性下载

我正在做一个项目,我遇到了属性问题download:我发现Safari浏览器不支持它(根据文档).

这是一个片段:

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <a href="http://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded&a" download>Press to Download</a>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我搜索的方式用纯做到这一点Javascript还是Angular,但我没有找到什么.如果有人有解决方案会很棒.

javascript html5 angularjs

5
推荐指数
1
解决办法
86
查看次数

如何从具有匹配属性的数组中删除对象?

请考虑以下数据:

food = {
  id: 1,
  name: 'Pizza',
  price: 16
};

orders = [
  { food_id: 2, table_id: 5 },
  { food_id: 2, table_id: 5 },
  { food_id: 1, table_id: 5 },
  { food_id: 3, table_id: 5 },
  { food_id: 1, table_id: 5 }
];
Run Code Online (Sandbox Code Playgroud)

我想从orders数组匹配中删除单个项目food_id.这是我试过的:

removeFoodOrder(food: Food): void {
  for (let order of this.orders) {
    let match = this.orders.filter((order) => order.food_id == food.id);
    match ? this.orders.splice(this.orders.indexOf(order), 1) : null;
    break;
  }
  console.log(this.orders);
}
Run Code Online (Sandbox Code Playgroud)

如果我调用removeFoodOrder(food) …

typescript ecmascript-6 angular

5
推荐指数
1
解决办法
4万
查看次数

Angular:如何缓存图像?

我正在开发一个网络应用程序,我需要缓存图像,以便在用户关闭并再次打开网站时不需要时间加载。再次打开网站时,应该从缓存中加载图像,而不是再次重新加载。

这可以用 Angular 来实现吗?

我在网上搜索了很多,但还没有找到合适的解决方案。

javascript caching angular

5
推荐指数
1
解决办法
8315
查看次数

如何设置嵌套的formBuilder组的值

我的嵌套表单目前以这种方式格式化:

ngOnInit() {
  this.user = this.fb.group({
    name: ['', [Validators.required, Validators.minLength(2)]],
    quest1: ['', Validators.required],
    account: this.fb.group({
      email: ['', Validators.required],
      confirm: ['', Validators.required]
    }),
    questions: this.fb.group({
        quest2: ['', Validators.required],
    }),
  });
}
Run Code Online (Sandbox Code Playgroud)

我通常会设置这样的值:

this.user.controls['quest1'].setValue('false');

但是因为formGroups是嵌套的,所以我不确定如何设置嵌套值.

如何设置"quest2"的值?在嵌套的formGroup中设置表单控件值的正确语法是什么?

编辑:我正在使用这种方式

this.user.controls['quest1'].setValue('false');

forms typescript angular2-forms angular angular-reactive-forms

5
推荐指数
1
解决办法
2139
查看次数

Angular 4 i18n也可以获得TS/JS代码的翻译

我正在使用我的Angular 4项目的默认i18n进程,所以我ng-i18n用来生成.xlf没有问题.

但是根据我的打字稿代码,我还有一些文本需要翻译并通过id检索当前语言.

在Angular 4中我找不到任何方法.我错过了什么吗?如果那件事,我正在使用AOT.

如果服务器从Web服务发回一些消息/错误代码,如何使用Angular转换它们,也会出现此问题

internationalization angular2-aot angular

5
推荐指数
1
解决办法
1396
查看次数

角2中是否有独特的过滤器?

我有一个json如下:

[{
  "_id": "58a58ea23744cd46918914ef",
  "entityId": "ce7200c1-3430-47ce-ab81-7d0622cb8cae",
  "name": "Portugal",
  "description": "Portugal",
  "type": "country",
  "category": "common",
  "subcategory": "common",
  "parent": {
    "_id": "58a8486ca4890027348a8966"
  },
  "image": {
    "_id": "58a8486ca4890027348a8965"
  },
  "metadata": {
    "primary": "pt",
    "taxRate": 20,
    "exchangeRate": 0.7,
    "language": "PT",
    "currency": "EUR",
    "_id": "58a8486ca4890027348a8964"
  }
}, {
  "_id": "58a58ea23744cd46918914f1",
  "entityId": "815b9e90-e36f-4488-ad50-6a259b6d034d",
  "name": "Qatar",
  "description": "Qatar",
  "type": "country",
  "category": "common",
  "subcategory": "common",
  "parent": {
    "_id": "58a8486ca4890027348a8969"
  },
  "image": {
    "_id": "58a8486ca4890027348a8968"
  },
  "metadata": {
    "primary": "qa",
    "taxRate": 20,
    "exchangeRate": 0.7,
    "language": "RO",
    "currency": "RO",
    "_id": …
Run Code Online (Sandbox Code Playgroud)

pipe typescript angular

4
推荐指数
1
解决办法
2万
查看次数