簡體   English   中英

如何使用 Material 將 ng-messages 添加到 AngularJS 中動態創建的輸入

[英]How to add ng-messages to a dynamically created input in AngularJS with Material

除了我嘗試添加 ng-messages(它們不顯示)的部分之外,我有以下代碼可以工作。 根據我的理解,ng-messages 附加到 name 元素。 我在這里做錯了什么?

<form ng-submit="submit(form)" name="form">
   <div ng-repeat="question in questions">
      <div ng-switch="question.type">
         <div ng-switch-when="text">
            <md-input-container class="md-block">
               <label>{{question.title}}</label>
               <input md-maxlength={{question.maxLength}}
                      required md-no-asterisk
                      name="questionStorage[question.id]"
                      ng-model="questionStorage[question.id]">
               <div ng-messages="form[questionStorage[question.id]].$error">
                   <div ng-message="required">This is required.</div>
...

在 Lex 和Angular的幫助下, 將 ng-messages 動態設置為 name 屬性,我發現答案是:

<form ng-submit="submit(form)" name="form">
   <div ng-repeat="question in questions">
      <div ng-switch="question.type">
         <div ng-switch-when="text">
            <md-input-container class="md-block">
               <label>{{question.title}}</label>
               <input md-maxlength={{question.maxLength}}
                      required md-no-asterisk
                      name="question{{$index}}"
                      ng-model="questionStorage[question.id]">
               <div ng-messages="form['question' + $index].$error">
                   <div ng-message="required">This is required.</div>
...

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM