gpt4 book ai didi

javascript - Angular ng-click无法获取ng-model的值

转载 作者:行者123 更新时间:2023-12-02 15:40:25 25 4
gpt4 key购买 nike

我正在使用一个表单,我实际上只是想从 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>

这是我的整个 Controller 。最终结果是向 WordPress 发布评论,但是由于我的表单内容返回未定义,这有点困难。 (P.S. 它发布到 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 不会存在于您的 Controller 中。

你应该尝试这个:

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

希望有帮助。

关于javascript - Angular ng-click无法获取ng-model的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32632696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com