直到Angular的"最终"2.0我做到了这一点:
<input type="text" formControlName="name" [disabled]="!showName">
Run Code Online (Sandbox Code Playgroud)
动态禁用/启用表单输入.
从Rc7升级到2.0后,我在控制台窗口中收到此警告:
看起来您正在使用带有反应式表单指令的disabled属性.如果在组件类中设置此控件时将disabled设置为true,则实际将在DOM中为您设置disabled属性.我们建议使用此方法来避免"检查后更改"错误.
我已将我的代码更改为遵循这些说明,如下所示:
this._userGroupUsersForm = this._formBuilder.group({
'users': [{'', disabled: this.showName}, Validators.compose([Validators.required, Validators.minLength(3), Validators.maxLength(50), Validators.pattern("^[a-zA-ZåäöÅÄÖ 0-9_-]+$")])]
});
Run Code Online (Sandbox Code Playgroud)
这适用于初始页面加载,但我不能再像这样切换状态:
toggleName() : void { this.showName = !this.showName; }
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
注意:我的"旧"方式(通过设置[禁用])也不再起作用.
我正在使用EntityFramework和"代码优先"方法进行迁移.
我已经成功地从我的模型中生成了表格,但是这些列是按字母顺序添加的,而不是我模型中的顺序.
我试过这个:
[Key, Column(Order=0)]
public int MyFirstKeyProperty { get; set; }
[Column(Order=1)]
public int MySecondKeyProperty { get; set; }
Run Code Online (Sandbox Code Playgroud)
但这似乎并没有奏效.
如何手动设置数据库中字段的顺序?
我使用的是ASP.NET Core和EF Core(SqlServer)v1.1.0.
c# entity-framework entity-framework-core asp.net-core asp.net-core-1.0
我有一个Angular 2 RC7应用程序,我使用SystemJS加载JavaScript文件.
这是我对SystemJS的当前配置:
(function (global) {
System.config({
defaultExtension: 'js',
defaultJSExtensions: true,
paths: {
'npm:': 'node_modules/'
},
// Let the system loader know where to look for things
map: {
// Our app is within the app folder
app: 'app',
// Angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
// Other libraries
'rxjs': 'npm:rxjs',
'ng2-translate': 'node_modules/ng2-translate'
},
// Tell the system loader how to load when no filename and/or no …Run Code Online (Sandbox Code Playgroud) 如何OPTIONS在针对跨域API 的请求中添加标头?
我正在使用的API要求Authorization在所有请求中将JWT令牌设置为标头.
当我尝试访问API Angular时,首先执行一个OPTIONS不关心我为"真实"请求设置的标头的请求,如下所示:
this._headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer my-token-here'
});
return this._http
.post(AppConfig.apiUrl + 'auth/logout', params, {headers: this._headers})
...
...
Run Code Online (Sandbox Code Playgroud)
当没有提供令牌时,API返回HTTP状态401,Angular认为OPTIONS请求失败.
我有一个使用Identity的ASP.NET Core应用程序.它可以工作,但是当我尝试将自定义角色添加到数据库时,我遇到了问题.
在Startup中,ConfigureServices我添加了Identity和角色管理器作为范围服务,如下所示:
services.AddIdentity<Entities.DB.User, IdentityRole<int>>()
.AddEntityFrameworkStores<MyDBContext, int>();
services.AddScoped<RoleManager<IdentityRole>>();
Run Code Online (Sandbox Code Playgroud)
在Startup中,Configure我注入RoleManager并将其传递给我的自定义类RolesData:
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
RoleManager<IdentityRole> roleManager
)
{
app.UseIdentity();
RolesData.SeedRoles(roleManager).Wait();
app.UseMvc();
Run Code Online (Sandbox Code Playgroud)
这是RolesData班级:
public static class RolesData
{
private static readonly string[] roles = new[] {
"role1",
"role2",
"role3"
};
public static async Task SeedRoles(RoleManager<IdentityRole> roleManager)
{
foreach (var role in roles)
{
if (!await roleManager.RoleExistsAsync(role))
{
var create = await roleManager.CreateAsync(new IdentityRole(role));
if (!create.Succeeded)
{
throw new Exception("Failed …Run Code Online (Sandbox Code Playgroud) 我有一个Angular 2应用程序,我在其中为某些输入设置默认值,如下所示:
this._stdSearchForm = this._formBuilder.group({
count: [{value: 50, disabled: false}, Validators.compose([Validators.required, Validators.minLength(1), Validators.maxLength(3), Validators.pattern("^[0-9]+$")])]
});
Run Code Online (Sandbox Code Playgroud)
我已经添加了一个"重置"功能,如下所示:
(click)="resetStdSearchForm()"
Run Code Online (Sandbox Code Playgroud)
那只是运行:
this._stdSearchForm.reset();
Run Code Online (Sandbox Code Playgroud)
这会重置表单,但会忽略FormBuilder组中定义的初始值.
这种行为是有意的吗?
重置表单后,我可以以编程方式设置"count"的值吗?我试过这样做:
this._stdSearchForm.value.count = 50;
Run Code Online (Sandbox Code Playgroud)
但这没有改变.
我有一个调用API的服务,如下所示:
return this._http
.post(appSettings.apiUrl + 'SomeController/SomeAction', params, {withCredentials: true, headers: this.headers})
.timeoutWith(appSettings.maxTimeHttpCalls, Observable.defer(() => Observable.throw(this._feedbackService.timeout())))
.map((response: Response) => response.json().data);
Run Code Online (Sandbox Code Playgroud)
现在我想在该调用上实现一个过滤函数rxjs/add/operator/filter,但是我无法让它正常工作.
这是我采取的方法:
return this._http
.post(appSettings.apiUrl + 'SomeController/SomeAction', params, {withCredentials: true, headers: this.headers})
.timeoutWith(appSettings.maxTimeHttpCalls, Observable.defer(() => Observable.throw(this._feedbackService.timeout())))
.filter(response => response.json().data.Type === 5)
.map((response: Response) => response.json().data);
Run Code Online (Sandbox Code Playgroud)
但无论我过滤ngFor什么,只要过滤器在那里,循环就不会产生任何效果.如果我删除它,一切都按预期工作.
我应该在之前或之后添加过滤器map吗?
我可以像这样过滤响应JSON,还是需要使用其他语法?
示例JSON
以下是JSON响应的示例:
data: [
{
"Type": 5,
"Description": "Testing",
"ID": "001525"
}
]
Run Code Online (Sandbox Code Playgroud) 我的RC5应用程序中出现此错误:
承诺拒绝:在注入Router之前引导至少一个组件.
main.ts:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
Run Code Online (Sandbox Code Playgroud)
app.module.ts:
@NgModule({
imports: [
BrowserModule,
routing,
SharedModule.forRoot()
],
declarations: [
AppComponent,
ErrorComponent
],
bootstrap: [AppComponent]
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
shared.module.ts:
@NgModule({
imports: [CommonModule, RouterModule, HttpModule, FormsModule, ReactiveFormsModule],
declarations: [TranslatePipe, DateToStringPipe, HeaderComponent, FooterComponent],
exports: [TranslatePipe, DateToStringPipe, CommonModule, FormsModule, ReactiveFormsModule, HeaderComponent, FooterComponent]
})
export class SharedModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: SharedModule,
providers: [
FeedbackService,
CookieService,
AuthService,
LoggerService,
RouteGuard,
{
provide: TranslateLoader, …Run Code Online (Sandbox Code Playgroud) 如果HTTP调用发生超时事件,我想向用户提供反馈.
我试过这个:
return this._http
.post(apiUrl + 'Search/Everything', params, {withCredentials: true, headers: this.headers})
.timeout(5000, this._feedbackService.error("Custom error message"))
.map((response: Response) => response.json().data);
Run Code Online (Sandbox Code Playgroud)
但是,只要进行HTTP调用,就会触发反馈服务.
超时启动时如何启动服务?
我正在反对的API为我提供了以下结构:
"data": [
{
"id": 5,
"name": "First name",
"parent": 0
},
{
"id": 1,
"name": "Second name",
"parent": 5
},
{
"id": 6,
"name": "Third name",
"parent": 1
},
{
"id": 15,
"name": "Fourth name",
"parent": 0
},
{
"id": 25,
"name": "Fifth name",
"parent": 5
}
]
Run Code Online (Sandbox Code Playgroud)
我希望围绕此构建一个树结构,使用ngFor它支持无限数量的子级别.
这是我到目前为止所尝试的:
<div *ngFor="let d1 of _dataList">
<ul *ngIf="d1.parent == 0">
<li>
{{d1.name}}
<ul *ngFor="let d2 of _dataList">
<li *ngIf="d2.parent == d1.id">{{d2.name}}</li>
</ul>
</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
这是有效的,但它很难看,我必须手动重复这个X级别的数据,从而留下硬编码限制.
如何优化此代码以支持无限级别 - …
在我的 Angular RC2 应用程序中,我进行了一个可观察的 HTTP 调用,该调用从 API 向我返回以下 JSON:
{
"success":true,
"data":
{
"Type": 1
"Details":{
"Id":"123",
"Name":"test",
"Description":"test"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我这样映射数据:
this._myService.get(id)
.subscribe(
(data) => {
this.details = data.Details;
this.type = data.Type;
},
(error) => {
this.setError(error);
}
);
Run Code Online (Sandbox Code Playgroud)
如何从这里访问“详细信息”对象内的值?
我试过:
{{details.Name}}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,我也不能使用 ngFor 来循环它。