小编AJT*_*T82的帖子

如何从实现ControlValueAccessor的组件引用FormControl?

我创建了一个源自此博客文章的Angular组件.我有一个被动的形式,我想在窗体控件上得到错误,组件本身将有一个程式化的错误消息,它将在控件有错误时呈现.但是,当我尝试将NgControl类注入组件时,我得到循环引用问题,那么我将如何访问控件上的错误?

这是当前的代码,它还没有完成,但它应该给出我想要完成的基本想法:

import { Component, Output, EventEmitter, Input, forwardRef } from '@angular/core';
import {
    NgControl,
    NG_VALUE_ACCESSOR,
    ControlValueAccessor,
    Validator,
    AbstractControl,
    FormControl,
    NG_VALIDATORS
} from '@angular/forms';

@Component({
    selector: 'form-field-input',
    templateUrl: './form-field-input.component.html',
    styleUrls: ['./form-field-input.component.less'],
    providers: [{
        provide: NG_VALUE_ACCESSOR,
        useExisting: forwardRef(() => FormFieldInputComponent),
        multi: true
    }]
})
export class FormFieldInputComponent implements ControlValueAccessor {

    private propagateChange = (_: any) => { };
    private propagateTouch = (_: any) => { };

    @Input('label') label: string;
    @Input('type') type: string;
    @Input('id') id: string;
    @Input('formControlName') formControlName: string;
    @Input('error') …
Run Code Online (Sandbox Code Playgroud)

angular

7
推荐指数
1
解决办法
2033
查看次数

具有多个参数和注入项的TypeScript构造函数

我有一个打字稿类如下:

export class ImageItem{
    constructor(public Id: number, public ImageUrl: string, public Caption: string, public Description: string, private _sanitizer: DomSanitizer)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

所有构造函数的参数都是与该类相关的属性,但最后一项是注入我的类的类.我的问题是,如果我想创建这个类的对象,我想做一些如下所示的事情:

var a=new ImageItem(1, 'image1', 'http://aaa.com/aa.jpg', '')
Run Code Online (Sandbox Code Playgroud)

但是由于注入的项目,它给了我一个错误.我想知道如何解决这个问题.

typescript angular

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

TS:转义保留关键字并用作变量名

希望这不是重复的,一直在四处寻找,甚至没有真正找到这是否可能的答案,更不用说如何了,如果可以的话。

我的问题是,我正在使用变量 name 将传入的 json 放到我的前面,我"is-saved"如何在相应的 Typescript 类中使用它,或者我可以吗?所以我正在寻找我在后端(Java)中做的类似解决方案:

@JsonProperty("is-saved")
private Boolean isSaved;
Run Code Online (Sandbox Code Playgroud)

是的,我会从你那里得到答案,只是更改名称,但我不能,我无权访问该实际代码。

我找到了一些建议,并尝试了以下方法,但不起作用:

is-saved: Boolean //obviously!
"is-saved": Boolean
'is-saved': Boolean
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮忙吗?

typescript

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

"@ angular/http/index没有HTTP_PROVIDERS的导出成员"

依赖

在此输入图像描述

main.ts文件

在此输入图像描述

我想导入,HTTP_PROVIDERS但是给了我错误

"@ angular/http/index没有HTTP_PROVIDERS的导出成员"

我附上了package.json和main.ts文件的图像.

typescript visual-studio-code angular

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

为什么我们不在 Angular 组件中使用关键字“function”?

在 ts 文件中,

export class ComponentName{
   methodName (para1,para2) {
     //code here
   }
}
Run Code Online (Sandbox Code Playgroud)

这被认为是正确的,但是当我将关键字 function 添加到方法时,代码不会编译并给出错误,说它只需要方法、访问器、构造函数和属性。

angular

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

即使模型更改,Angular 4 复选框状态也不会更新

我知道这个问题仍然存在,但根据这个解决方法Angular 2 - Checkbox not keep in sync它应该可以按预期工作,但是我仍然很难让它适用于我的案例。

我有授予角色的权限列表。当用户想要更新角色时,我会显示带有可编辑角色名称的表单及其权限列表作为复选框列表。现在,如果角色已经拥有列表中的权限,我需要检查一些列表项。

我的表格:

<tr *ngFor ="let perm of permissions, let i = index">
        <td>
            <label>
            <input type="checkbox" 
                    name="check-box-{{perm.permission_alias}}"                                            
                    value="{{perm.permission_id}}"  
                    (ngModel)="perm.checked"
                    (ngModelChange)="onSelectFilter($event,perm)"
                    attr.id="check-box-{{perm.permission_alias}}"
                    /> 
            {{perm.permission_alias}}
            </label>                                
        </td>
    </tr>
Run Code Online (Sandbox Code Playgroud)

我的组件:

getPermissions(role: Role): void {
    this.permissions = [];
    this.userService.getPermissions().then(perms => {            
        if (role != null) {
            this.permissions = perms;
            for (let rp of role.permissions) {          
                let index = this.permissions.indexOf(this.permissions.find(p => p.permission_id == rp.permission_id));
                if (index >= 0) {  
                     this.permissions[index].checked = true;
                    //this.onSelectFilter(true, this.permissions[index]); not …
Run Code Online (Sandbox Code Playgroud)

checkbox typescript angular2-forms angular

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

Angular2 - 错误:无法解析IconService的所有参数

我一直在尝试将我的应用程序切换到AoT编译,并在应用程序加载时在生产环境中收到此错误(它在本地工作正常).

Error: Can't resolve all parameters for IconService: (?, ?)

似乎错误来自提供IconService的模块.图标服务构造函数看起来像

constructor(private http:Http, private iconConfiguror:IconConfiguror) {

所以我的问题是这个错误是什么意思,为什么它只会在prod环境中发生(我尝试在本地启用prod模式)?

现在看来似乎意味着,不设置http和图标的配置参数,但在应用程序模块水平设置的图标配置和HttpModule在所述进口IconModule,其中IconService被提供.

@NgModule({
    imports: [
        CommonModule,
        HttpModule,
    ],
    declarations: [
        IconComponent,
    ],
    exports: [
        IconComponent,
    ],
    providers: [
        IconService,
        __platform_browser_private__.BROWSER_SANITIZATION_PROVIDERS,
    ],
})
Run Code Online (Sandbox Code Playgroud)

和我们的图标组件的桶.

export * from "./components/icon/icon.configuror";

export * from "./components/icon/icon.service.provider";

export * from "./components/icon/icon.service";

export * from "./components/icon/icon.component";

export * from "./components/icon/icon.module";
Run Code Online (Sandbox Code Playgroud)

typescript angular

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

谷歌云端点正文数组

我们有一个用Java编写的rest API(在Wildfly中托管).我们的服务在kubernetes(GKE)运行.我们希望利用Cloud Endpoints来跟踪API的使用情况和响应能力.API不是新的,我们已经提供与其交互多年的软件.它也很大(成千上万的公共方法).我们有API的Swagger文档,没有验证错误.当我尝试使用以下方式部署Swagger:

gcloud beta service-management deploy swagger.yaml
Run Code Online (Sandbox Code Playgroud)

它没有成功.我得到以下错误重复237次:

ERROR: unknown location: http: body field path 'body' must be a non-repeated message.
Run Code Online (Sandbox Code Playgroud)

我已经将它跟踪到237个方法,其中包括一个body参数中的json数组.在我们的API中,这些是接受或返回对象列表的方法.有什么方法可以让我接受service-management deploy吗?更改我们的API不是一个选项,但我们真的希望能够使用端点.

例如,此方法签名:

@PUT
@Path ("/foobars/undelete")
@Consumes (MediaType.APPLICATION_JSON)
@Produces (MediaType.APPLICATION_JSON)
@ApiOperation (value = "Undelete foobars")
@ApiResponses (value =
{
    @ApiResponse (
        code              = 200,
        message           = "foobars undeleted",
        response          = FooBar.class,
        responseContainer = "List"
    ) , @ApiResponse (
        code              = 206,
        message           = "Not all foobars undeleted",
        response          = FooBar.class,
        responseContainer = "List"
    ) , @ApiResponse …
Run Code Online (Sandbox Code Playgroud)

google-app-engine google-cloud-endpoints kubernetes

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

Uncaught TypeError: angular.module is not a function at

这是在app.js

var angular = angular.module('ngApp', ['ngRoute']);
angular.config(function($routeProvider) {
$routeProvider
    .when('/', {
        templateUrl: '../templates/companies.html',
        controller: 'companies'
    })
    .........
});
Run Code Online (Sandbox Code Playgroud)

这是在companies.js

angular.module('ngApp').controller('companies', ['$scope', function($scope) {
    .........
}]);
Run Code Online (Sandbox Code Playgroud)

这是在index.html

<head>
    <title>Basic demo</title>
    <script src="angular.js"></script>
    <script src="angular-route.js"></script>
    <script src="js/app.js" type="text/javascript"></script>
    <script src="js/Controllers/companies.js" type="text/javascript" ></script>
</head>
Run Code Online (Sandbox Code Playgroud)

我不断收到此错误:

未捕获的类型错误:angular.module 不是 company.js:1 的函数

我究竟做错了什么?

angularjs

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

Angular.io/Angular 4.如何在触发另一个组件视图时刷新它

我有一个简单的Angular.io应用程序.(angular-cli/4.1.0)

我有一个NavbarComponent,用于呈现用户名.

第一次访问应用程序时我没有登录,我的应用程序重定向到LoginComponent.我的NavBar也被渲染但没有用户名.成功登录后,我被重定向到我的HomeComponent.

这就是问题所在.我的NavBar不显示用户名.但是,如果我执行刷新/ ctrl + r,则会呈现用户名.

怎么了?

app.component.html

<nav-bar></nav-bar>
<router-outlet></router-outlet>
Run Code Online (Sandbox Code Playgroud)

navbar.compoment.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'nav-bar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {

  me;

  ngOnInit() {
    this.me = JSON.parse(localStorage.getItem('currentUser'));
  }
}
Run Code Online (Sandbox Code Playgroud)

login.component.ts

import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';

import { AlertService, AuthenticationService } from '../_services/index';

@Component({
    moduleId: module.id,
    templateUrl: 'login.component.html'
})

export class LoginComponent implements OnInit {
    model: any = {};
    loading …
Run Code Online (Sandbox Code Playgroud)

angular2-routing angular

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