admin管理员组文章数量:1415653
I wrote some code to create an simple todo app in angular js. I have the following function code to remove the tasks from the list.
Javascript code
$scope.deleteTask = function(){
$scope.tasks.splice(this.$index, 1);
if($scope.tasks.length < 1){
$scope.noTask = true;
}
};
HTML code
<li ng-repeat="task in tasks track by $index">{{task}} <button ng- click="deleteTask()">x</button></li> </li>
<p ng-show="noTask">No Tasks Available </p>
I wanted to show a message when there are no tasks in the list. i have achieved this using "if" statement. but i don't need an else here. i am not sure whether its the right way. what will be the proper way to achieve this
I wrote some code to create an simple todo app in angular js. I have the following function code to remove the tasks from the list.
Javascript code
$scope.deleteTask = function(){
$scope.tasks.splice(this.$index, 1);
if($scope.tasks.length < 1){
$scope.noTask = true;
}
};
HTML code
<li ng-repeat="task in tasks track by $index">{{task}} <button ng- click="deleteTask()">x</button></li> </li>
<p ng-show="noTask">No Tasks Available </p>
I wanted to show a message when there are no tasks in the list. i have achieved this using "if" statement. but i don't need an else here. i am not sure whether its the right way. what will be the proper way to achieve this
Share Improve this question asked Apr 21, 2016 at 6:21 ShaSha 1,9946 gold badges35 silver badges60 bronze badges 5- What is not working ? Looks good.... – Rayon Commented Apr 21, 2016 at 6:25
-
3
The
else
part of an if-statement is optional. Your code is correct. – Lucas S. Commented Apr 21, 2016 at 6:26 - it is working fine. but can i write a "if" statement without "else". is it a good practice? @RayonDabre – Sha Commented Apr 21, 2016 at 6:27
- 2 of course it's good. Did you consider googling that before posting it here? It should be explained in a lot of places. – Mike B Commented Apr 21, 2016 at 6:28
-
It is perfectly fine.. But I would initialize
$scope.noTask = something
initially...depending on the length of the$scope.tasks
– Rayon Commented Apr 21, 2016 at 6:28
3 Answers
Reset to default 3There is nothing wrong with your code.
You can use the if
statement without the else
.
In your case I would remend writing it as follows to remove some unnecessary code:
<p ng-show="tasks.length==0">No Tasks Available </p>
As in any (?) other programming language you can omit else
part if it's empty.
Just make sure that if you assign some value in if (true)
than if it's not executed have default value for that variable:
var test = false;
if (Math.rand() < 0.5) {
test = true;
}
Also there is shorthand
for if else (where you can't omit else
part):
var test = Math.rand() < 0.5 ? "True value" : "False value";
I wanted to show a message when there are no tasks in the list. i have achieved this using "if" statement. but i don't need an else here. i am not sure whether its the right way. what will be the proper way to achieve this
You don't need an else
statement, you can use ng-if
.
The ng-if
statement is also available without an else
condition.
You should be able to use this angular code:
$scope.deleteTask = function(){
$scope.tasks.splice(this.$index, 1);
};
And this part for the HTML:
<li ng-repeat="task in tasks track by $index">{{task}}
<button ng-click="deleteTask()">x</button>
</li>
<p ng-show="noTask" ng-if="tasks.length == 0">No Tasks Available </p>
本文标签: htmlCan we write IF statement without else in javascriptStack Overflow
版权声明:本文标题:html - Can we write IF statement without else in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745166463a2645710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论