• <tfoot id='rwhov'></tfoot>
    <legend id='rwhov'><style id='rwhov'><dir id='rwhov'><q id='rwhov'></q></dir></style></legend>
    <i id='rwhov'><tr id='rwhov'><dt id='rwhov'><q id='rwhov'><span id='rwhov'><b id='rwhov'><form id='rwhov'><ins id='rwhov'></ins><ul id='rwhov'></ul><sub id='rwhov'></sub></form><legend id='rwhov'></legend><bdo id='rwhov'><pre id='rwhov'><center id='rwhov'></center></pre></bdo></b><th id='rwhov'></th></span></q></dt></tr></i><div id='rwhov'><tfoot id='rwhov'></tfoot><dl id='rwhov'><fieldset id='rwhov'></fieldset></dl></div>

    <small id='rwhov'></small><noframes id='rwhov'>

          <bdo id='rwhov'></bdo><ul id='rwhov'></ul>
      1. AngularJS:初始复选框值不在模型中

        时间:2024-04-19
        • <small id='1P1fp'></small><noframes id='1P1fp'>

        • <legend id='1P1fp'><style id='1P1fp'><dir id='1P1fp'><q id='1P1fp'></q></dir></style></legend>
          <i id='1P1fp'><tr id='1P1fp'><dt id='1P1fp'><q id='1P1fp'><span id='1P1fp'><b id='1P1fp'><form id='1P1fp'><ins id='1P1fp'></ins><ul id='1P1fp'></ul><sub id='1P1fp'></sub></form><legend id='1P1fp'></legend><bdo id='1P1fp'><pre id='1P1fp'><center id='1P1fp'></center></pre></bdo></b><th id='1P1fp'></th></span></q></dt></tr></i><div id='1P1fp'><tfoot id='1P1fp'></tfoot><dl id='1P1fp'><fieldset id='1P1fp'></fieldset></dl></div>

            <bdo id='1P1fp'></bdo><ul id='1P1fp'></ul>

            <tfoot id='1P1fp'></tfoot>

                <tbody id='1P1fp'></tbody>

                  本文介绍了AngularJS:初始复选框值不在模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个带有这样复选框的表单:

                  I have got a form with a checkbox like this:

                  <input type="checkbox" name="publish" id="publish" ng-model="data.publish" ng-true-value="Yes" ng-false-value="No"/>
                  

                  现在我想将其初始值 'No'(因为它未选中)绑定到模型,而无需单击复选框.除了在控制器中手动设置模型值或使用 ngInit 之外,还有其他方法吗?

                  Now I would like to have its initial value 'No' (becuase it is unchecked) bound to the model without having to click on the checkbox. Is there a way besides setting the model value manually in the controller or using ngInit?

                  我的脚本应该适用于不同的形式,因此手动设置模型变量是不可行的.而且 ngInit 方法看起来相当不优雅.

                  My script should work with different forms so manually setting model variables is no option. And the ngInit method seems rather inelegant.

                  推荐答案

                  可能是这样的指令:

                  <input type="checkbox" ng-model="..." init-cb ... />
                  

                  下面的代码应该做到这一点:

                  The following code should do it:

                  m.directive("initCb", function() {
                      return {
                          require: ["ngModel"],
                          scope: false,
                          link: function(scope, element, attrs, controllers) {
                              var ngModel = controllers[0],
                              if( element[0].checked ) {
                                  ngModel.$setViewValue(attrs.ngTrueValue || true);
                              }
                              else {
                                  ngModel.$setViewValue(attrs.ngFalseValue || false);
                              }
                          }
                      };
                  });
                  

                  这篇关于AngularJS:初始复选框值不在模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将属性动态分配给 JS 对象 下一篇:JQuery 事件不在生产中的 heroku 上工作,而是在开发中工作

                  相关文章

                  <small id='bXqAU'></small><noframes id='bXqAU'>

                • <tfoot id='bXqAU'></tfoot>
                  <legend id='bXqAU'><style id='bXqAU'><dir id='bXqAU'><q id='bXqAU'></q></dir></style></legend>

                    1. <i id='bXqAU'><tr id='bXqAU'><dt id='bXqAU'><q id='bXqAU'><span id='bXqAU'><b id='bXqAU'><form id='bXqAU'><ins id='bXqAU'></ins><ul id='bXqAU'></ul><sub id='bXqAU'></sub></form><legend id='bXqAU'></legend><bdo id='bXqAU'><pre id='bXqAU'><center id='bXqAU'></center></pre></bdo></b><th id='bXqAU'></th></span></q></dt></tr></i><div id='bXqAU'><tfoot id='bXqAU'></tfoot><dl id='bXqAU'><fieldset id='bXqAU'></fieldset></dl></div>
                        <bdo id='bXqAU'></bdo><ul id='bXqAU'></ul>