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

时间:2015-09-17 14:20:06

标签: javascript angularjs

我正在使用表单,我只是想从ng模型中获取值。表格如下:

<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然而我的表单内容返回undefined它有点困难。 (P.S.它发布到wordpress和评论只是说&#39; undefined&#39;):

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

})

我的控制台日志显示:当我点击发送时未定义?

1 个答案:

答案 0 :(得分:2)

我很确定模态有一个孤立的范围。这意味着您的控制器中不存在$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>

希望它有所帮助。