【问题标题】:Angularjs model not updating from view input field? [duplicate]Angularjs模型没有从视图输入字段更新? [复制]
【发布时间】:2016-07-04 15:48:02
【问题描述】:

我试图从输入字段中获取用户名并传递给函数以创建用户帐户,但该字段中的用户名输入似乎在模型中更新。

我使用 window.alert 来显示用户名只是为了测试目的。

login.html

ion-view view-title="Login">
    <ion-content>

         <div class="list list-inset" padding>

            <div class="item-divider" align="center"> Login</div>

            <label class="item item-input">
                <span class="input-label">UserName</span>
                <input  ng-model="username" type="text" placeholder="Email">
            </label>

            <label class="item item-input">
                <span class="input-label">Password</span>
                <input  ng-model="password" type="password" placeholder="Your Password">
            </label>  

        </div>
         <center><button class="button button-positive " ng-click="log()">Login</button> </center>

app.js(不是整个代码)

blog.config(function($stateProvider,$urlRouterProvider){
    $stateProvider
        .state('mainlist',{
        url:'/mainlist',
        templateUrl:'templates/MainList.html',
        controller:'listCtrl'
    })
        .state('login',{
        url:'/login',
        templateUrl:'templates/login.html',
        controller:'loginCtrl'
        });
blog.controller('loginCtrl',function($scope)
                {
                        $scope.log=function()
                        {
                            var username= $scope.username;
                            window.alert(username);
                        }

                    }
               );

【问题讨论】:

标签: angularjs ionic-framework


【解决方案1】:

您需要首先定义模型。将您的代码更改为此,它应该可以工作(未经测试):

blog.config(function($stateProvider,$urlRouterProvider){
    $stateProvider
        .state('mainlist',{
        url:'/mainlist',
        templateUrl:'templates/MainList.html',
        controller:'listCtrl'
    })
        .state('login',{
        url:'/login',
        templateUrl:'templates/login.html',
        controller:'loginCtrl'
        });


blog.controller('loginCtrl',function($scope)
                {

                        $scope.username = "";
                        $scope.log=function()
                        {
                            var username= $scope.username;
                            window.alert(username);
                        }

                    }
               );

【讨论】:

    【解决方案2】:
    <label class="item item-input">
                    <span class="input-label">UserName</span>
                    <input  ng-model="data.username" type="text" placeholder="Email">
     </label>
    

    app.js

    blog.controller('loginCtrl',function($scope)
                {
                        $scope.data={};
                        $scope.log=function()
                        {
                            var username= $scope.data.username;
                            window.alert(username);
                        }
    
                    }
               );
    

    【讨论】:

    • 在定义控制器时更好地使用controllerAs 模式
    • 我刚学Angularjs,谢谢指教
    猜你喜欢
    • 2019-12-15
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2015-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多