如何为AMP未初始化变量设置默认值?

时间:2017-06-23 13:07:13

标签: amp-html

<input type="checkbox" [required]="(currentState == null) ? (true) : (false)">

amp-bind:[required]的默认值与第一个表达式result(true)不匹配。这可能会在下一次状态更改后导致意外行为。

编辑:也许更多的背景可以帮助

<amp-state id="privacySettings">
      <script type="application/json">{                                   
           "general": {
               "mp": "required",
               "p1": false,
               "p5": false
           },
           "specific": {
               "mp": false,
               "p1": "required",
               "p5": "required",
               "ck": false
           }
       }
   </script>
</amp-state>
<div class="privacy">
    <div id="pmaindiv">
        <input on="tap:err_mess.hide" role="mainmandatoryprivacy" tabindex="1" type="checkbox" name="main-privacy" id="pmain" class="pmain" value="1"  [required]=privacySettings[currentState].mp  [checked]=privacySettings[currentState].ck >

每次尝试分配给[require](但它也代表[checked],我猜,对于其他属性)一个值(静态或通过var)会得到bind-amp错误。

<amp-state id="temp">
     <script type="application/json">
          {
           "var": "general"
            }
     </script>
</amp-state>

(...)  [required]="(currentState==null) ? privacySettings[temp.var].mp : privacySettings[currentState].mp" (...)

如果通过'on'属性设置currentState,它会通过验证并正常工作。

1 个答案:

答案 0 :(得分:3)

您是否设置了required属性以及[required]绑定?

试试这个:

<input type="checkbox" 
       [required]="(currentState == null ? 'true' : 'false')" 
       required="true">

另外,如果未定义变量,则可以使用此

(myVar || 'defaultVal')

如果未定义myVar,则会改为使用值defaultVal

但设置默认值的最正确方法是使用<amp-state>,如下所示:

  <amp-state id="props">
    <script type="application/json">
      {
        "myVar": "blah"
      }
    </script>
  </amp-state>

然后你可以使用你给id元素的amp-state作为前缀来使用该变量:

props.myVar


编辑:以下为我工作

注意:
1.在初始currentState元素中设置amp-state
2.基本required属性的默认值

<amp-state id="privacySettings">
    <script type="application/json">
    {   
      "currentState":"general",                              
      "general": {
        "mp": "required",
        "p1": false,
        "p5": false
      },
      "specific": {
        "mp": false,
        "p1": "required",
        "p5": "required",
        "ck": false
      }
     }
 </script>
</amp-state>
<div class="privacy">
    <div id="pmaindiv">
        <input role="mainmandatoryprivacy" tabindex="1" type="checkbox" name="main-privacy" id="pmain" class="pmain" value="1"  
        required="required"
        [required]=privacySettings[privacySettings.currentState].mp
        >

  </div>
</div>

编辑2:当currentState尚未初始化时,以下内容也应该有效:

<amp-state id="privacySettings">
    <script type="application/json">
    {                              
      "general": {
        "mp": "required",
        "p1": false,
        "p5": false
      },
      "specific": {
        "mp": false,
        "p1": "required",
        "p5": "required",
        "ck": false
      }
     }
 </script>
</amp-state>


<amp-state id="temp">
     <script type="application/json">
          {
           "var": "general"
            }
     </script>
</amp-state>

<div class="privacy">
    <div id="pmaindiv">
        <input role="mainmandatoryprivacy" tabindex="1" type="checkbox" name="main-privacy" id="pmain" class="pmain" value="1"  
        required="required"
        [required]=(currentState==null?privacySettings[temp.var].mp:currentState) 
        >

  </div>
</div>