【问题标题】:angular $scope.variable is not updating using a ng-model directive on inputangular $scope.variable 未在输入上使用 ng-model 指令进行更新
【发布时间】:2016-01-22 13:09:46
【问题描述】:

我正在开发一个带有视图的 Angular 应用程序,用户可以在其中输入可以设置他的电话号码的输入。

 <div class="modal">
      <ion-header-bar  class="actu_header">
      <h1 class="title">Nouvelles Infos</h1>
      <div class="button button-clear" style="width:50px" ng-click="cancel()"><span class="icon ion-chevron-down"></span></div>
    </ion-header-bar>
    <ion-content  class="padding_header modalPhone">
    <h4> Entrez votre numéro de téléphone pour confirmer votre compte</h4>
    <label class="item item-input">
      <span class="input-label"><i class="ion-ios-telephone"></i> </span>
      <input type="tel" ng-model="telephone" placeholder="Entrez votre numero de mobile"/>
    </label>
    <p class="err_container" ng-if="err">{{err}}</p>
    <button ng-click="sendPhoneNumber()" class="button homepage_button icon-right ">
      Valider
  </button>
  </ion-content>
</div>

当用户点击按钮时。它应该将电话数据发送到服务器。这是前面模态依赖的相关控制器:

    .controller('RegisterCtrl', function($scope, $http, $location, $localStorage,$ionicLoading, $ionicHistory, mySock, user,$cordovaNetwork, $ionicModal){
  $ionicHistory.clearCache();
  $ionicHistory.clearHistory();
  $scope.err = "";
  $scope.user={};
  // $scope.telephone = "";

  $scope.launchModal = function(){
    $ionicModal.fromTemplateUrl('templates/phoneConfirmation.html', {
      scope: $scope,
      animation: 'slide-in-up'
    }).then(function(modal) {
      $scope.modal2 = modal;
      $scope.modal2.show();
    });
  }

  $scope.cancel = function(){
    $scope.goBack(-1);
    $scope.modal2.hide();
  }

  // $scope.telephone = "";

  $scope.sendPhoneNumber = function(){
    console.log($scope.telephone);
    $http.post(serverAddress+'/user/sendConfirmationSms', {telephone: $scope.telephone}).success(function(data){})
  }
  $scope.launchReq = function(){
    if($scope.err)
      delete $scope.err;
    if(window.device && $cordovaNetwork.isOffline()){
      error_reporter.show({texte: "Connectez vous d'abord à internet!"});
    }
    else{
      $ionicLoading.show({
        content: 'Loading Data',
        animation: 'fade-out',
        showBackdrop: false,
        hideOnStateChange: true
      });
      $http.post(serverAddress+'/user/create',$scope.user).success(function(data){
       $localStorage.set('token',data[0].token);
       $localStorage.setObject('user',data[0]);
       $localStorage.setObject('friends',[]);
       user.getCoord();
       mySock.req(serverAddress+'/connexion/setSocket',{id: data[0].id}); //Link socket_id with the user.id
       $location.path('/user/profil');
     }).error(function(err){
      $ionicLoading.hide();
      $scope.err = "Erreur veuillez vérifier que tous les champs sont remplis et que l'adresse mail n'est pas déjà utilisée.";
    });
   }
 }
})

无论我在输入字段中键入什么,打印到控制台的 $scope.telephone 的值始终是一个空字符串。 因此,即使我在输入标签上指定了 ng-model="telephone",但实际上并未将电话的值发送到服务器。

---更新---

html的问题和平实际上是通过在registerCtrl中单击具有函数launchModal的元素触发的模态。 通过从 RegisterCtrl 断开我的模态并将其链接到一个新的控制器,它终于起作用了:

<div class="modal" ng-controller ="PhoneCtrl">
        <ion-header-bar  class="actu_header">
      <h1 class="title">Nouvelles Infos</h1>
      <div class="button button-clear" style="width:50px" ng-click="cancel()"><span class="icon ion-chevron-down"></span></div>
    </ion-header-bar>
     <ion-content  class="padding_header modalPhone">
    <h4> Entrez votre numéro de téléphone pour confirmer votre compte</h4>
    <label class="item item-input">
      <span class="input-label"><i class="ion-ios-telephone"></i> </span>
      <input type="tel" ng-model="$parent.telephone" placeholder="Entrez votre numero de mobile"/>
    </label>
    <p class="err_container" ng-if="err">{{err}}</p>
    <button ng-click="sendPhoneNumber()" class="button homepage_button icon-right ">
      Valider
  </button>
  </ion-content>
</div>

这是新的控制器

.controller('PhoneCtrl', function($scope, $http, $location, $localStorage,$ionicLoading, $ionicHistory, mySock, user,$cordovaNetwork, $ionicModal){ 

  $scope.cancel = function(){
    $scope.goBack(-1);
    $scope.modal2.hide();
  }

  $scope.sendPhoneNumber = function(){
    alert($scope.telephone)
    console.log($scope.telephone);
    $http.post(serverAddress+'/user/sendConfirmationSms', {telephone: $scope.telephone}).success(function(data){})
  }
 })

但我仍然不明白为什么它不能在原始控制器中工作..如果有人有想法?

【问题讨论】:

  • 您确定是绑定不起作用而不是服务器调用?如果您在$http.post 上方添加console.log($scope.telephone),电话号码是否会按照您的预期记录到控制台?
  • 我相信您没有将 div 绑定到控制器。
  • @Rhumborl no 当我尝试console.log($scope.telephone时,我唯一得到的是一个空字符串,无论如何
  • 请完整发布您的 HTML 和 JS 文件。我在控制台上获得了适当的价值。再试一次我的回答。
  • @PratapA.K,刚刚做到了

标签: angularjs angular-ngmodel


【解决方案1】:

由于该指令在 Ionic 源代码中定义的方式,问题是特定于 ion-content 的。它专门创建自己的子范围。

我可以通过将输入绑定到 $parent.telephone

改变这个

ng-model="telephone"

到此

ng-model="$parent.telephone"

所以

<ion-content  class="padding_header modalPhone">
    <h4> Entrez votre numéro de téléphone pour confirmer votre compte</h4>
    <label class="item item-input">
      <span class="input-label"><i class="ion-ios-telephone"></i> </span>
      <input type="tel" ng-model="$parent.telephone" placeholder="Entrez votre numero de mobile"/>
    </label>
    <p class="err_container" ng-if="err">{{err}}</p>
    <button ng-click="sendPhoneNumber()" class="button homepage_button icon-right ">
      Valider
  </button>
  </ion-content>

您可以在此处找到更多详细信息

problem-with-ion-content-and-scope-vars-inside-function

根据您的要求,看看下面

我的 Index.html 和控制器

<!DOCTYPE html>
<html ng-app="routerApp">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="//code.angularjs.org/1.4.8/angular.js"></script>
<script src="//rawgithub.com/g00fy-/angular-datepicker/1.0.3/dist/index.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.min.js"></script>
<script>

var routerApp = angular.module('routerApp', ['ui.router', 'ionic', 'ngCordova']);

routerApp.controller('RegisterCtrl', ['$scope', '$http', '$location', '$ionicLoading', '$ionicHistory','$cordovaNetwork', '$ionicModal', 
                                      function($scope, $http, $location, $ionicLoading, $ionicHistory,$cordovaNetwork, $ionicModal){
    $scope.telephone = "";

      $scope.sendPhoneNumber = function(){
          alert("--"+$scope.telephone);
        console.log($scope.telephone);
        /* $http.post(serverAddress+'/user/sendConfirmationSms', {telephone: $scope.telephone}).success(function(data){}) */
      }

    }]);

</script>
</head>
<body ng-controller="RegisterCtrl">
    <div class="modal">
        <ion-header-bar  class="actu_header">
      <h1 class="title">Nouvelles Infos</h1>
      <div class="button button-clear" style="width:50px" ng-click="cancel()"><span class="icon ion-chevron-down"></span></div>
    </ion-header-bar> 
     <ion-content  class="padding_header modalPhone">
    <h4> Entrez votre numéro de téléphone pour confirmer votre compte</h4> 
    <label class="item item-input">
      <span class="input-label"><i class="ion-ios-telephone"></i> </span>
      <input type="tel" ng-model="$parent.telephone" placeholder="Entrez votre numero de mobile"/>
    </label>
    <p class="err_container" ng-if="err">{{err}}</p>
    <button ng-click="sendPhoneNumber()" class="button homepage_button icon-right ">
      Valider
  </button>
  </ion-content> 
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ionic/1.2.4/js/ionic.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ng-cordova/0.1.23-alpha/ng-cordova.min.js"></script>
<script src="cordova.js"></script>
</body>
</html>

确保您将下载并在您的 html 文件中引用cordova.js。

【讨论】:

  • 当我 console.log($scope.telephone) 无论我的输入标签中有什么内容时,我总是得到一个空字符串
  • 以上代码对我有用!!我正在控制台上输入值。
  • 嘿 Pratap,感谢您的回答,但我仍然无法更新 console.log 我的电话值(它不知道 $parent 服务...)你能告诉我你如何更新控制器端的代码吗?
  • 当我尝试 console.log 电话时仍然得到空白。但是当我直接在视图中显示 {{telephone}} 时,它正在工作
  • 您是否尝试执行我的代码(index.html)?我在控制台上输入值。我没有遇到任何问题。如果仍然需要帮助,请创建一个 jsfiddle/Plunker 并提供链接。
【解决方案2】:

这是您使用的总代码吗?如果是,那么您错过了 $http.post() 调用中的 'config' 变量。

config 应该包含上面代码中没有包含的头文件!

如果您已将其包含在您的代码中,请分享实际文件。

【讨论】:

    【解决方案3】:

    如果你确定你的请求 json 是这样的,

    {
    telephone:$scope.telephone
    }
    

    尝试以下操作:

    1) 使用服务上方的console.log($scope.telephone) 来检查它的值是否正确。

    2) 尝试像这样编辑您的 json `

    $http.post(serverAddress+'/user/sendConfirmationSms', 
    {
    "telephone": $scope.telephone
    })
    .success(function(data){})
    

    3) 检查您的服务 url 和端口号(这是重要的一般错误)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      相关资源
      最近更新 更多