Javascript 如果文本区域为空,则返回假角度代码

Javascript 如果文本区域为空,则返回假角度代码,javascript,angularjs,Javascript,Angularjs,var myApp=angular.module(“myApp”,[]); 控制器(“votesController”,['$scope',函数($scope,$timeout){ $scope.comments=[ ]; $scope.newComment={ 喜欢:0 }; $scope.createComment=function(){ 如果($scope.newcommon.comment!=“”){ $scope.comments.push({ 注释:$scope.newComme

var myApp=angular.module(“myApp”,[]);
控制器(“votesController”,['$scope',函数($scope,$timeout){
$scope.comments=[
];
$scope.newComment={
喜欢:0
};  
$scope.createComment=function(){
如果($scope.newcommon.comment!=“”){
$scope.comments.push({
注释:$scope.newComment.comment,
喜欢:$scope.newComment.likes,
像颜色:{},
不喜欢颜色:{}
});
}
};
$scope.incrementLikes=函数(注释){
comment.likes++;
};
$scope.decrementLikes=函数(注释){
评论;
};  
}]);

喜欢
|{{comment.likes}}}
不像
{{comment.comment}
添加文本

您可以这样做:

$scope.incrementLikes = function(comment) {
   if(comment.comment != null && comment.comment.length > 0)  comment.likes++;
};

$scope.decrementLikes = function(comment) {
    if(comment.comment != null && comment.comment.length > 0)  comment.likes--;
};

你可以这样做:

$scope.incrementLikes = function(comment) {
   if(comment.comment != null && comment.comment.length > 0)  comment.likes++;
};

$scope.decrementLikes = function(comment) {
    if(comment.comment != null && comment.comment.length > 0)  comment.likes--;
};
您首先检查了文本是否为空。如果文本为空,请不要执行递增或递减操作


您首先检查了文本是否为空。如果文本为空,请不要执行递增或递减操作。

您应该初始化$scope.newComment.comment以使其不未定义

var myApp=angular.module(“myApp”,[]);
控制器(“votesController”,['$scope',函数($scope,$timeout){
$scope.comments=[
];
$scope.newComment={
喜欢:0,
评论:“”
};  
$scope.createComment=function(){
如果($scope.newcommon.comment!=“”){
$scope.comments.push({
注释:$scope.newComment.comment,
喜欢:$scope.newComment.likes,
像颜色:{},
不喜欢颜色:{}
});
}
};
$scope.incrementLikes=函数(注释){
comment.likes++;
};
$scope.decrementLikes=函数(注释){
评论;
};  
}]);

喜欢
|{{comment.likes}}}
不像
{{comment.comment}
添加文本

您应该初始化$scope.newComment.comment,使其不未定义

var myApp=angular.module(“myApp”,[]);
控制器(“votesController”,['$scope',函数($scope,$timeout){
$scope.comments=[
];
$scope.newComment={
喜欢:0,
评论:“”
};  
$scope.createComment=function(){
如果($scope.newcommon.comment!=“”){
$scope.comments.push({
注释:$scope.newComment.comment,
喜欢:$scope.newComment.likes,
像颜色:{},
不喜欢颜色:{}
});
}
};
$scope.incrementLikes=函数(注释){
comment.likes++;
};
$scope.decrementLikes=函数(注释){
评论;
};  
}]);

喜欢
|{{comment.likes}}}
不像
{{comment.comment}
添加文本

使用
if($scope.newComment.comment.trim()){
检查注释是否包含任何字符串。使用
if($scope.newComment.comment.trim()){
检查注释是否包含任何字符串。非常感谢您的帮助:)在JS中我可以使用.val(“”)要在添加注释后清除文本区域,在Angular中也是类似的选项吗?您对问题的描述让其他人误解了。下次可能会更清楚地描述它。@V.Rjust use$scope.newComment.comment=“”,然后@V.r非常感谢您的帮助:)在JS中我可以使用.val(“”)若要在添加注释后清除文本区域,在Angular中也是类似选项吗?您对问题的描述使其他人误解了。下次可能会更清楚地描述它。@V.R必须使用$scope.newComment.comment=”“然后@V.R