簡體   English   中英

角ng-click無法獲得ng-model的值

[英]Angular ng-click cannot get value of ng-model

我正在使用表單,而我只是想從ng-model中獲取值。 表單如下所示:

<form name="comment_form" class="row" novalidate>
    <div class="col col-80 content col-center">
    <input class="new-comment-message" type="text" style="margin-left: 15px;" placeholder="Leave a comment..." ng-model="new_comment" required></input>
    </div>
    <div class="col col-20 button-container col-center">
    <button class="button button-clear send" type="submit" ng-click="addComment()" ng-disabled="comment_form.$invalid">
        Send
    </button>
    </div>
</form>

這是我的整個控制器。 最終結果是在wordpress上發布評論,但是由於我的表單內容返回未定義狀態,這有點困難。 (請注意,將其發布到wordpress時,評論只是說“未定義”):

.controller('EasternInnerCtrl', function ($http, $timeout, $scope, $ionicLoading, $stateParams, $ionicScrollDelegate, $cordovaSocialSharing, $ionicModal, Easternc, AuthService) {
  $scope.eastc = Easternc.get($stateParams.eastcId);



  $ionicModal.fromTemplateUrl('commenter.html', {
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function(modal) {
    $scope.modal = modal
  })  

  $scope.openModal = function() {
    $scope.modal.show()
  }

  $scope.closeModal = function() {
    $scope.modal.hide();
  };

  $scope.$on('$destroy', function() {
    $scope.modal.remove();
  });




  $scope.addComment = function(){
    $ionicLoading.show({
      template: 'Submiting comment...'
    });
    console.log($scope.new_comment);
    Easternc.submitComment($scope.eastc.id, $scope.new_comment).then(function(data){
      if(data.status=="ok"){
        var user = AuthService.getUser();

        var comment = {
          author: {name: user.data.username},
          content: $scope.new_comment,
          date: Date.now(),
          user_gravatar : user.avatar,
          id: data.comment_id
        };
        console.log($scope.new_comment);
        /*$scope.eastc.comments.push(comment);
        console.log(comment);

        $scope.new_comment = "";
        $scope.new_comment_id = data.comment_id;
        $ionicLoading.hide();
        $ionicScrollDelegate.scrollBottom(true);*/
      }
    });
  };

  $scope.sharePost = function(link){ 
    console.log(link);
    window.plugins.socialsharing.share('I just read this article on blah: ', null, null, url);
  };

})

我的控制台日志顯示:單擊“發送”時未定義?

我很確定模態有一個孤立的范圍。 這意味着$scope.new_comment存在於您的控制器中。

您應該嘗試這樣:

$scope.addComment = function(comment){
    Easternc.submitComment($scope.eastc.id,comment).then(function(data){
        console.log(comment); 
    });
};

在您的HTML中

<button class="button button-clear send" type="submit" ng-click="addComment(new_comment)" ng-disabled="comment_form.$invalid">
    Send
</button>

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM