在我的test.js控制器中,if / else语句注销了一个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);
}
}
});
这是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>
如果没有if语句,则每次调用cardClick()时,Cards数组都会将两个对象推入其中。第一个对象是输入的ng模型,第二个对象总是空字符串。
示例:
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:""}]
包含if语句将产生所需的结果
示例:
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"}]
我的问题是为什么if语句会带来这个结果,为什么test.js中的if / else语句会在每次调用cardClick()时注销if和else结果。
修改
这是没有if / else的代码。这将在第一个示例中呈现结果。
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);
}
});
答案 0 :(得分:0)
这一行:
console.log($scope.card);
出现在if
声明和else
声明中。所以你会看到以任何一种方式记录。
对于if/else
问题,您是否可以将代码if/else
移除,以便我们进行比较?