我正在遵循官方的角“英雄”快速入门教程,同时尝试TDD它。
https://angular.io/docs/ts/latest/tutorial/toh-pt1.html
当我进入替换步骤时:
<input value="{{hero.name}}" placeholder="name">
与
<input [(ngModel)]="hero.name" placeholder="name">
我的Karma test runner抛出以下错误:
错误:模板分析错误:无法绑定到“ng model”,因为它不是“input”的已知属性。(“name:][(ngModel)]=”hero.name“placeholder=”name“>”):定义AppComponent@6:23需要未定义。
但是,应用程序按预期工作,我在控制台中没有看到任何错误。(而且我很自信我正确地遵循了教程,看不到任何错别字等等)
My App.Components.ts看起来像:
import { Component } from '@angular/core';
export class Hero {
id: number;
name: string;
}
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<h2>{{hero.name}} details!</h2>
<div><label>id: </label>{{hero.id}}</div>
<div>
<label>name: </label>
<input [(ngModel)]="hero.name" placeholder="name">
</div>
`
})
export class AppComponent {
title = 'Tour of Heroes';
hero: Hero = {
id: 1,
name: 'Windstorm'
};
}
我的app.module看起来像:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [ BrowserModule, FormsModule],
declarations: [ AppComponent],
bootstrap: [ AppComponent ]
})
export class AppModule { }
一旦我从模板中移除ngModel,Karma就能正常工作,而且正如我之前所说的,实际应用程序的行为与预期的一样。我在搜索了一遍后,似乎找不到任何与我的问题有关的信息:(
我的karma.config是标准的快速入门:
// #docregion
module.exports = function(config) {
var appBase = 'app/'; // transpiled app JS and map files
var appSrcBase = 'app/'; // app source TS files
var appAssets = 'base/app/'; // component assets fetched by Angular's compiler
var testBase = 'testing/'; // transpiled test JS and map files
var testSrcBase = 'testing/'; // test source TS files
config.set({
basePath: '',
frameworks: ['jasmine'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'), // click "Debug" in browser to see it
require('karma-htmlfile-reporter') // crashing w/ strange socket error
],
customLaunchers: {
// From the CLI. Not used here but interesting
// chrome setup for travis CI using chromium
Chrome_travis_ci: {
base: 'Chrome',
flags: ['--no-sandbox']
}
},
files: [
// System.js for module loading
'node_modules/systemjs/dist/system.src.js',
// Polyfills
'node_modules/core-js/client/shim.js',
'node_modules/reflect-metadata/Reflect.js',
// zone.js
'node_modules/zone.js/dist/zone.js',
'node_modules/zone.js/dist/long-stack-trace-zone.js',
'node_modules/zone.js/dist/proxy.js',
'node_modules/zone.js/dist/sync-test.js',
'node_modules/zone.js/dist/jasmine-patch.js',
'node_modules/zone.js/dist/async-test.js',
'node_modules/zone.js/dist/fake-async-test.js',
// RxJs
{ pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false },
{ pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false },
// Paths loaded via module imports:
// Angular itself
{ pattern: 'node_modules/@angular/**/*.js', included: false, watched: false },
{ pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false },
{ pattern: 'systemjs.config.js', included: false, watched: false },
{ pattern: 'systemjs.config.extras.js', included: false, watched: false },
'karma-test-shim.js',
// transpiled application & spec code paths loaded via module imports
{ pattern: appBase + '**/*.js', included: false, watched: true },
{ pattern: testBase + '**/*.js', included: false, watched: true },
// Asset (HTML & CSS) paths loaded via Angular's component compiler
// (these paths need to be rewritten, see proxies section)
{ pattern: appBase + '**/*.html', included: false, watched: true },
{ pattern: appBase + '**/*.css', included: false, watched: true },
// Paths for debugging with source maps in dev tools
{ pattern: appSrcBase + '**/*.ts', included: false, watched: false },
{ pattern: appBase + '**/*.js.map', included: false, watched: false },
{ pattern: testSrcBase + '**/*.ts', included: false, watched: false },
{ pattern: testBase + '**/*.js.map', included: false, watched: false }
],
// Proxied base paths for loading assets
proxies: {
// required for component assets fetched by Angular's compiler
"/app/": appAssets
},
exclude: [],
preprocessors: {},
// disabled HtmlReporter; suddenly crashing w/ strange socket error
reporters: ['progress', 'kjhtml'],//'html'],
// HtmlReporter configuration
htmlReporter: {
// Open this file to see results in browser
outputFile: '_test-output/tests.html',
// Optional
pageTitle: 'Unit Tests',
subPageTitle: __dirname
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
})
}
有人能帮我吗?
当使用testbed
配置测试时,就像为测试环境从头配置@ngmodule
一样。因此,当将AppComponent
添加到声明
中,并且AppComponent
需要forms指令时,您需要将FormsModule
导入到测试床配置中,就像在AppModule
中所做的那样
TestBed.configureTestingModule({
declarations: [ AppComponent ],
imports: [ FormsModule ]
})
我正在遵循官方的角“英雄”快速入门教程,同时试图TDD它。 https://angular.io/docs/ts/latest/tutorial/toh-pt1.html 我一到替换的步骤: 与 我的Karma测试运行者抛出以下错误: 有人能帮我吗?
我正在尝试测试控件的angular2双向绑定。以下是错误: app.component.html app.component.ts app.component.spec.ts
这似乎是一个流行的问题,但解决方案都建议导入FormsModule,这是正在做的。 app.module.shared.ts如下所示: app.module.client.ts文件如下所示: 如果有人能指出我做错了什么,我将非常感激,但我找到的每一篇文章都表明,通过导入FormsModule可以解决这个问题。
完整代码为: app.module为: 怎么了,为什么对我不起作用?
我正在使用Angular 6并试图呈现一个页面,我得到以下错误不能绑定到'ng模型',因为它不是'input'的已知属性。我已经尝试了堆栈溢出中的所有选项。 以下是选择 app.module.ts 我使用的是Visual Studio代码,当我点击一个特定的链接时,我得到了错误。请指导我。提前谢谢。
下面是项目类 我弄不清我做错了什么