【问题标题】:How to enable md-button from controller when it is having ng-disable expression?当控制器具有 ng-disable 表达式时,如何从控制器启用 md 按钮?
【发布时间】:2017-04-23 08:21:36
【问题描述】:

我的表单中有一个 md 按钮,我正在使用 ng-disable 表达式对其进行设置。一切正常。但现在我想在控制器的特定条件下启用按钮。 我是这样设置的。

vm.verifyMobileForm.$invalid = true;

但这对我不起作用。

这是我的元素

<form name="verifyMobileForm" novalidate>
    <md-input-container class="md-block" md-no-float>
        <input type="mobile" id="mobile" ng-value="{{vm.form.mobile}}" 
                name="mobile" ng-model="vm.form.mobile" 
                placeholder="Your Mobile number" 
                ng-pattern="/^[789]\d{9}$/" maxlength="10" required
        />
        <div ng-messages="verifyMobileForm.mobile.$error" role="alert" multiple>
            <div ng-message="required">
                <span >Mobile no. is required</span>
            </div>

        </div>

    </md-input-container>
    <div class="dialog-demo-content" layout="row" layout-wrap layout-margin layout-align="center">
        <md-button type="submit" 
              class="md-raised md-accent submit-button" 
              ng-disabled="verifyMobileForm.$invalid || verifyMobileForm.$pristine" 
              ng-click = "vm.checkMobile($event, document.getElementById('mobile').value)"
        >
            Next
        </md-button>
    </div>
</form>

这是我的控制器,我想将其设置为 true

(function ()
{
    'use strict';

    angular
        .module('app.pages.auth.verify-mobile')
        .controller('VerifyMobileController', VerifyMobileController);

    /** @ngInject */
    function VerifyMobileController($scope,dataservice,msApi, $state,$mdDialog)
    {
        var vm = this;
        vm.form = {};
        var getMob = dataservice.getData();
        if(getMob){
            vm.form.mobile = getMob[1].mobile;
            vm.verifyMobileForm.$invalid = true;
        }
    }
}();

【问题讨论】:

    标签: javascript angularjs angular-material angular-directive


    【解决方案1】:

    您说要在特定条件下启用按钮? 那么为什么不使用这样的东西

    ng-disabled="(verifyMobileForm.$invalid || verifyMobileForm.$pristine) && DISABLEBUTTON"

    ...

    在控制器中,即使表单无效,您也可以启用按钮

    $scope.DISABLEBUTTON = false;

    【讨论】:

    • 非常感谢@Andreas
    猜你喜欢
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 2014-09-08
    • 2011-09-29
    • 1970-01-01
    相关资源
    最近更新 更多