为什么我的所有数据验证都没有按设计验证?

时间:2017-01-09 19:17:46

标签: angularjs

我正在开发Angular 2中的这个应用程序,它有一个可以填充的表单,如果表单没有填充,它会提示你需要填充什么。它适用于客户名称,但其余部分,例如,"需要一个工具列表"一旦人口稠密就不会消失。以下是app / proposal / proposal-new.component.html下的代码:

<h1>Create a Proposal</h1>

<div>
    <form #proposalForm="ngForm">
        <div class="form-group">
            <label for="customer">Customer Name</label>
            <input type="text" id="customer" placeholder="ABC Supply" required name="customer" #customer='ngModel' [(ngModel)]="proposal.customer">
        <div [hidden]="customer.valid || customer.pristine">
            Customer name is required
        </div>
       </div>

        <div class="form-group">
                <label for="portfolio_url">Portfolio URL</label>
                <input type="text" id="portfolio_url" placeholder="ABC Supply" required name="portfolio_url" #portfolio_url='ngModel' [(ngModel)]="proposal.portfolio_url">
            <div [hidden]="portfolio_url.valid || portfolio_url.pristine">
                A Portfolio URL is required
            </div>
        </div>
        <div class="form-group">
                <label for="portfolio_url">Tools that you'll use on the project</label>
                <input type="text" id="portfolio_url" placeholder="Ruby on Rails, Angular, etc" required name="tools" #portfolio_url='ngModel' [(ngModel)]="proposal.tools ">
            <div [hidden]="tools.valid || tools.pristine">
                A list of tools is required
            </div>
        </div>
        <div class="form-group">
                <label for="estimated_hours">Estimated hours</label>
                <input type="number" id="estimated_hours" required name="estimated_hours" #portfolio_url='ngModel' [(ngModel)]="proposal.estimated_hours ">
            <div [hidden]="estimated_hours.valid || estimated_hours.pristine">
                You need to enter your estimated hours for the project
            </div>
        </div>
        <div class="form-group">
                <label for="hourly_rate">Hourly rate</label>
                <input type="number" id="hourly_rate" required name="hourly_rate" #portfolio_url='ngModel' [(ngModel)]="proposal.hourly_rate ">
            <div [hidden]="hourly_rate.valid || hourly_rate.pristine">
                You need to enter your hourly rate for the project
            </div>
        </div>
        <div class="form-group">
                <label for="weeks_to_complete">Weeks to Complete</label>
                <input type="number" id="weeks_to_complete" required name="weeks_to_complete" #weeks_to_complete='ngModel' [(ngModel)]="proposal.weeks_to_complete ">
            <div [hidden]="weeks_to_complete.valid || weeks_to_complete.pristine">
                You need to enter the weeks you estimate to complete the project
            </div>
        </div>
        <div class="form-group">
                <label for="weeks_to_complete">Client Email <em>(Optional)</em></label>
                <input type="email" id="weeks_to_complete" name="weeks_to_complete" #client_email='ngModel' [(ngModel)]="proposal.client_email">
        </div>
    </form>
<div>

    <p>Hi {{proposal.customer}},</p>

    <p>It was a pleasure getting to meet with you and review your project requirements, I believe it is a great fit for the types of applications that I build out. Please feel free to check out some of my past projects <a href="{{proposal.portfolio_url}}">here.</a></p>

    <p>To successfully build out the application I will be utilizing {{proposal.tools}}, and a number of other tools to ensure that the project follows industry wide best practices.</p>

    <p>Ensuring that you are fully informed is one of my top priorities, so in addition to incorporating everything we discussed, I will also be creating a project management dashboard and sending you a project update message daily so that you can have a transparent view of the development as it takes place.</p>

    <p>To accomplish the project and meet the requirements that we discussed, I am estimating that it will take {{proposal.estimated_hours}} hours in development time to complete. The project should take {{proposal.weeks_to_complete}} weeks to complete and with my hourly rate of {{proposal.hourly_rate}}/hour, the estimated total will be {{proposal.hourly_rate * proposal.estimated_hours}}.</p>

    <p>The next step from here is to set up a meeting to finalize the project and sign contracts.</p>

    <p>I am excited to working with you and build out this project.</p>
</div>

以下是app / proposal / proposal.ts:

export class Proposal {
    constructor(
        public id?: number,
        public customer?: string,
        public portfolio_url: string = 'http://',
        public tools?: string,
        public estimated_hours?: number,
        public hourly_rate?: number,
        public weeks_to_complete?: number,
        public client_email?: string,
    ) {}
}

如果还有其他文件我应该查看和发布,请告诉我。我在这里难过。

2 个答案:

答案 0 :(得分:2)

您看到的问题是多次定义局部变量portfolio_url而不是每个输入的新局部变量。如果更新局部变量定义,如下所示:#[var_name]='ngModel',则每个验证的唯一名称应按预期工作。

将您的HTML更新为以下内容:

<h1>Create a Proposal</h1>

<div>
    <form #proposalForm="ngForm">
        <div class="form-group">
            <label for="customer">Customer Name</label>
            <input type="text"
                   id="customer"
                   placeholder="ABC Supply"
                   required name="customer"
                   #customer='ngModel'
                   [(ngModel)]="proposal.customer">
        <div [hidden]="customer.valid || customer.pristine">
            Customer name is required
        </div>
       </div>

        <div class="form-group">
                <label for="portfolio_url">Portfolio URL</label>
                <input type="text"
                       id="portfolio_url"
                       placeholder="ABC Supply"
                       required
                       name="portfolio_url"
                       #portfolio_url='ngModel'
                       [(ngModel)]="proposal.portfolio_url">
            <div [hidden]="portfolio_url.valid || portfolio_url.pristine">
                A Portfolio URL is required
            </div>
        </div>
        <div class="form-group">
                <label for="portfolio_url">Tools that you'll use on the project</label>
                <input type="text"
                       id="tools"
                       placeholder="Ruby on Rails, Angular, etc"
                       required
                       name="tools"
                       #tools='ngModel'
                       [(ngModel)]="proposal.tools ">
            <div [hidden]="tools.valid || tools.pristine">
                A list of tools is required
            </div>
        </div>
        <div class="form-group">
                <label for="estimated_hours">Estimated hours</label>
                <input type="number"
                       id="estimated_hours"
                       required
                       name="estimated_hours"
                       #estimated_hours='ngModel'
                       [(ngModel)]="proposal.estimated_hours ">
            <div [hidden]="estimated_hours.valid || estimated_hours.pristine">
                You need to enter your estimated hours for the project
            </div>
        </div>
        <div class="form-group">
                <label for="hourly_rate">Hourly rate</label>
                <input type="number"
                       id="hourly_rate"
                       required
                       name="hourly_rate"
                       #hourly_rate='ngModel'
                       [(ngModel)]="proposal.hourly_rate ">
            <div [hidden]="hourly_rate.valid || hourly_rate.pristine">
                You need to enter your hourly rate for the project
            </div>
        </div>
        <div class="form-group">
                <label for="weeks_to_complete">Weeks to Complete</label>
                <input type="number"
                       id="weeks_to_complete"
                       required
                       name="weeks_to_complete"
                       #weeks_to_complete='ngModel'
                       [(ngModel)]="proposal.weeks_to_complete ">
            <div [hidden]="weeks_to_complete.valid || weeks_to_complete.pristine">
                You need to enter the weeks you estimate to complete the project
            </div>
        </div>
        <div class="form-group">
                <label for="weeks_to_complete">Client Email <em>(Optional)</em></label>
                <input type="email"
                       id="weeks_to_complete"
                       name="weeks_to_complete"
                       #client_email='ngModel'
                       [(ngModel)]="proposal.client_email">
        </div>
    </form>
<div>

    <p>Hi {{proposal.customer}},</p>

    <p>It was a pleasure getting to meet with you and review your project requirements, I believe it is a great fit for the types of applications that I build out. Please feel free to check out some of my past projects <a href="{{proposal.portfolio_url}}">here.</a></p>

    <p>To successfully build out the application I will be utilizing {{proposal.tools}}, and a number of other tools to ensure that the project follows industry wide best practices.</p>

    <p>Ensuring that you are fully informed is one of my top priorities, so in addition to incorporating everything we discussed, I will also be creating a project management dashboard and sending you a project update message daily so that you can have a transparent view of the development as it takes place.</p>

    <p>To accomplish the project and meet the requirements that we discussed, I am estimating that it will take {{proposal.estimated_hours}} hours in development time to complete. The project should take {{proposal.weeks_to_complete}} weeks to complete and with my hourly rate of {{proposal.hourly_rate}}/hour, the estimated total will be {{proposal.hourly_rate * proposal.estimated_hours}}.</p>

    <p>The next step from here is to set up a meeting to finalize the project and sign contracts.</p>

    <p>I am excited to working with you and build out this project.</p>
</div>

Working Plunkr

答案 1 :(得分:0)

你不应该这样做:

#portfolio_url='ngModel' [(ngModel)]="proposal.portfolio_url"

在Angular 2文档中,他们说:

  

&#34;电话&#34;的哈希(#)前缀意味着我们正在定义手机变量

因此,在您的情况下,您在代码中多次定义portfolio_url。您需要使用哈希前缀,因为portfolio_url已在proposal对象中定义。

以下内容应该有效(read more about the template binding syntax):

<input type="number" id="hourly_rate" required name="hourly_rate" [(value)]="proposal.hourly_rate">

或者你可以这样做:

<input type="number" id="hourly_rate" required name="hourly_rate" #proposal.hourly_rate>

angular 2 docs还有另一个用户输入示例:https://angular.io/docs/ts/latest/guide/user-input.html