2015-05-03 58 views
0

$ scope.property值我有以下離子模板中定義:離子ngModel指令不更新在控制器

<ion-view view-title="Home" hide-nav-bar="true"> 
    <ion-content class="padding"> 
    <label class="item item-input"> 
     <input type="text" placeholder="Enter Name" ng-model="name"> 
    </label> 
    <button class="ion-plus-circled button button-block button-positive" ng-click="add()" side="right"> 
     Add Name 
    </button> 
</ion-content> 
</ion-view> 

我的控制器看起來如下:

//home controller 
.controller('HomeCtrl',['$scope','$log', 
    function($scope,$log){ 
     $scope.name = null; 
     $scope.add = function(){ 
     var name = $scope.name; 
     if(name != null && name != undefined){ 
      $log.debug('participant added to list:' + name); 
     }else{ 
      //notify the user that they must enter a valid name 
      $log.debug('user entered invalid name'); 
     } 
    }; 
}]) 

add()上調用$scope$scope.name值爲空。

因此,上面的代碼和標記會導致以下內容被記錄。

user entered invalid name

爲何不拾取器文本框中的值?

+0

提後參加的所有服務()方法 –

+0

這裏工作得很好:http://plnkr.co/edit/bpKfghCBgxNx4jTUl3C1?p = preview – rmuller

+0

@MukundKumar我刪除了對「參與者」的引​​用,因爲它與這篇文章無關。謝謝。 – TheJediCowboy

回答

1

使用此代碼:

HTML:

<button class="ion-plus-circled button button-block button-positive" ng-click="add(name)" side="right"> 
    Add Name 
</button> 

控制器:

$scope.add = function(name){ 
     if(name != null && name != undefined){ 
      $log.debug('participant added to list:' + name); 
     }else{ 
      //notify the user that they must enter a valid name 
      $log.debug('user entered invalid name'); 
     } 
    }; 

OR:

HTML:

<input type="text" placeholder="Enter Name" ng-model="obj.name"> 

控制器:

$scope.add = function(){ 
     var name = $scope.obj.name; 
     if(name != null && name != undefined){ 
      $log.debug('participant added to list:' + name); 
     }else{ 
      //notify the user that they must enter a valid name 
      $log.debug('user entered invalid name'); 
     } 
    };