加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 角度2:无法读取未定义的属性“验证器”

发布时间:2020-12-17 07:44:08 所属栏目:安全 来源:网络整理
导读:好吧,现在正式让我疯了 我的Angular 2应用程序中有两个组件,每个应用程序都包含一个表单.注册表单正常工作,但我的登录表单有问题.最近我一直使用注册组件作为登录组件的模板,只是为了尝试找到问题. 我已经从组件的类和组件的模板中修剪了除了最需要的代码之
好吧,现在正式让我疯了

我的Angular 2应用程序中有两个组件,每个应用程序都包含一个表单.注册表单正常工作,但我的登录表单有问题.最近我一直使用注册组件作为登录组件的模板,只是为了尝试找到问题.

我已经从组件的类和组件的模板中修剪了除了最需要的代码之外的所有代码.我的组件的模板目前看起来像这样:

<link href="css/animation.css" rel="stylesheet">

<section id="signin_alt" class="authenty signin-alt">
    <form [ngFormModel]="signinForm" novalidate>
    </form>
</section>

我的组件的类看起来像这样:

import { Component } from 'angular2/core';
import { ControlGroup,FormBuilder,AbstractControl,Validators } from 'angular2/common';
import { RouterOutlet,RouterLink,RouteConfig,Router,ROUTER_DIRECTIVES } from 'angular2/router';
import { AuthenticationService } from './services/authentication.service';
import { AppComponent } from '../app.component';

@Component({
    selector: 'signin',directives: [RouterOutlet,RouterLink],template: require('./signin.component.html')
})
export class SigninComponent {
    signinForm: ControlGroup;
}

如果我从表单中删除了[ngFormModel],它不会抛出任何错误.当我添加属性,我得到这个美味的结果:

EXCEPTION: TypeError: Cannot read property 'validator' of undefined in [signinForm in SigninComponent@3:14]

为了完整起见,这里是我的webpack config:

var sliceArgs = Function.prototype.call.bind(Array.prototype.slice);
var toString = Function.prototype.call.bind(Object.prototype.toString);
var path = require('path');
var webpack = require('webpack');

module.exports = {
    devtool: 'source-map',debug: true,devServer: {
        historyApiFallback: true,contentBase: 'src/public',publicPath: '/__build__'
    },entry: {
        'app': './src/app/bootstrap','vendor': './src/app/vendor.ts'
    },output: {
        path: root('public/__build__'),filename: '[name].js',sourceMapFilename: '[name].js.map',chunkFilename: '[id].chunk.js'
    },resolve: {
        extensions: ['','.ts','.js','.json','.css','.html']
    },module: {
        loaders: [
            { test: /.json$/,loader: 'json' },{ test: /.css$/,loader: "style-loader!css-loader" },{ test: /.png$/,loader: "url-loader?limit=100000" },{ test: /.jpg$/,loader: "file-loader" },{ test: /.html$/,loader: 'html' },{
                test: /.ts$/,loader: 'ts',exclude: [/.spec.ts$/,/.e2e.ts$/,/node_modules/]
            },{
                test: /.scss$/,loaders: ['style','css?sourceMap','sass?sourceMap']
            },{
                test: /.(woff|woff2)(?v=[0-9].[0-9].[0-9])?$/,loader: "url-loader?limit=10000&minetype=application/font-woff"
            },{
                test: /.(ttf|eot|svg)(?v=[0-9].[0-9].[0-9])?$/,loader: "file-loader"
            }
        ]
    },plugins: [
        new webpack.optimize.CommonsChunkPlugin({ name: 'vendor',filename: 'vendor.bundle.js' }),new webpack.ProvidePlugin({ $: "jquery",jQuery: "jquery" })
    ]
};

function root(args) {
    args = sliceArgs(arguments,0);
    return path.join.apply(path,[__dirname].concat(args));
}

function rootNode(args) {
    args = sliceArgs(arguments,0);
    return root.apply(path,['node_modules'].concat(args));
}

查看堆栈跟踪,似乎表单没有被实例化.该角度函数中抛出错误:

NgFormModel.prototype.ngOnChanges = function (changes) {
    if (collection_1.StringMapWrapper.contains(changes,"form")) {
        var sync = shared_1.composeValidators(this._validators);
        this.form.validator = validators_1.Validators.compose([this.form.validator,sync]);
        var async = shared_1.composeAsyncValidators(this._asyncValidators);
        this.form.asyncValidator = validators_1.Validators.composeAsync([this.form.asyncValidator,async]);
        this.form.updateValueAndValidity({ onlySelf: true,emitEvent: false });
    }
    this._updateDomValue();
};

‘this.form’为空.

任何人都可以清楚这一点吗?我已经用尽了我对Angular 2的了解,寻找一个解决方案!

谢谢.

您使用ngFormModel指令后,在SigninComponent组件中的signinForm对象内实例化.这样的东西(例如):
constructor(_builder:FormBuilder) {
  this.signinForm = _builder.group({
        login: ['',Validators.required],password: ['',Validators.required]
});

希望它能帮助你,蒂埃里

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读