繁体   English   中英

使用vm作为控制器时如何设置输入字段的初始值?

[英]How to set initial value of input field when using vm as controller?

我正在使用john papa风格来开发我的角度应用程序,因此有控制器将数据传递给使用服务的另一个控制器。 在第二个控制器中,我想在控制器被调用时设置输入字段的值。 我试图将其设置为

vm.form.mobile = getMob [1] .mobile;

但这对我没有用。

这是我的第二位控制器

 (function ()
    {
        'use strict';

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

        /** @ngInject */
        function VerifyMobileController($scope,dataservice,msApi, $state,$mdDialog)
        {
            var vm = this;
            window.onbeforeunload = function() {
                $state.go('app.pages_auth_login');
            }
            var getMob = dataservice.getData();
            if(typeof getMob !== 'undefined'){

               vm.form.mobile = getMob[1].mobile;

            }
}
}();

这是html元素

<form name="verifyMobileForm" novalidate>
                <md-input-container class="md-block" md-no-float>
                    <input type="mobile" id="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>

尝试添加ng-value属性ng-value =“ {{vm.form.mobile}}”“

例如:

<input type="mobile" id="mobile" name="mobile" ng-value="{{vm.form.mobile}}" ng-model="vm.form.mobile" placeholder="Your Mobile number" ng-pattern="/^[789]\d{9}$/" maxlength="10" required>

确保在视图中使用controllerAs语法,如下所示:

<div ng-controller="VerifyMobileController as vm">

编辑:顺便说一句

 if(typeof getMob !== 'undefined')

当结果返回为null或NaN时,有一天可能会用脚射击您,更常见的写法是:

if(getMob)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM