简体   繁体   English

角ng-click无法获得ng-model的值

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

I am working with a form and I literally just want to get the value from an ng-model. 我正在使用表单,而我只是想从ng-model中获取值。 the form looks like this: 表单如下所示:

<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>

This is my whole controller. 这是我的整个控制器。 The end result is to post a comment to wordpress however With my form content returning undefined its a bit difficult. 最终结果是在wordpress上发布评论,但是由于我的表单内容返回未定义状态,这有点困难。 (PS its posting to wordpress and the comment is just saying 'undefined'): (请注意,将其发布到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);
  };

})

my console log is showing: undefined when I click send though? 我的控制台日志显示:单击“发送”时未定义?

I'm pretty sure the modal got an isolated scope. 我很确定模态有一个孤立的范围。 It means $scope.new_comment wont exists in your controller. 这意味着$scope.new_comment存在于您的控制器中。

You should try this : 您应该尝试这样:

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

with this in your html 在您的HTML中

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

Hope it helped. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM