2016-07-04 32 views
0

我; m試圖從輸入字段獲取用戶名並傳遞給函數以創建用戶帳戶,但字段中的用戶名輸入似乎在模型中更新。 enter image description hereAngularjs模型沒有從視圖輸入域更新?

我用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); 
         } 

        } 
       ); 
+1

參考此答案http://stackoverflow.com/questions/36177702/ionic-framework-scope-is-undefined-in-simple-alert –

+1

感謝Pankaj Parkar它的工作! – mhrzn

回答

0

您需要先定義模型。更改您的代碼這一點,它應該工作(未測試):

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); 
         } 

        } 
       ); 
0
<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); 
        } 

       } 
      ); 

enter image description here

+0

更好地使用'controllerAs'模式,同時定義控制器 –

+1

即時學習Angularjs,謝謝你的支持 – mhrzn

相關問題