javascript - ng-click function logs if and else statement -
in test.js controller if/else statement log out object ng-click
angular.module('stationeryapp') .factory('cards', function () { var cards = []; return cards }) .controller('testctrl', function ($scope, $log, cards) { $scope.cards = cards $scope.cardclick = function () { if ($scope.card['fname']!=="" && $scope.card['lname']!=="") { $scope.cards.push($scope.card) $scope.card={fname:'',lname:''} console.log($scope.card); } else { console.log($scope.card); } } });
this test.html:
<div> <md-toolbar layout='column' layout-align='center'> <md-button layout-margin layout-padding flex='100' class='md-raised md-primary'> <h1>{{ card.fname }}</h1> <h1>{{ card.lname }}</h1> </md-button> <div layout='column' layout-align='center center' > <input type="text" style="color:black;" flex='' ng-model="card.fname"> <input type="text" style="color:black;" flex='' ng-model="card.lname"> </div> </md-toolbar> </div>
without if statement cards array has 2 objects pushed every time cardclick() invoked. first object whatever ng-model inputs , second object always empty strings.
example:
fname input = "stack" lname input = "overflow" cardclick() result --> cards = [{fname:"stack", lname:"overflow"},{fname:"",lname:""}] fname input = "java" lname input = "script" cardclick() result --> cards = [{fname:"stack", lname:"overflow"},{fname:"",lname:""},{fname:"java",lname:"script"},{fname:"",lname:""}]
including if statement yield desired result
example:
fname input = "stack" lname input = "overflow" cardclick() result --> cards = [{fname:"stack", lname:"overflow"}] fname input = "java" lname input = "script" cardclick() result --> cards = [{fname:"stack", lname:"overflow"},{fname:"java",lname:"script"}]
my question why if statement bring result , why the if/else statement in test.js log out if , else result each time cardclick() invoked.
edit
here code without if/else. renders result in first example.
angular.module('stationeryapp') .factory('cards', function () { var cards = []; return cards }) .controller('testctrl', function ($scope, $log, cards) { $scope.cards = cards $scope.cardclick = function () { $scope.cards.push($scope.card) $scope.card={fname:'',lname:''} console.log($scope.card); } });
this line:
console.log($scope.card);
appears in both if
statement , else
statement. see logged either way.
as if/else
question, can provide code if/else
removed can compare?