我有一个小JSON对象,带有国家和城市。
重复电话给这个国家打电话。所以我现在希望,当我点击一个国家项目,这个项目显示的城市,并与另一个点击它切换回乡村。
我有两个功能,我可以把项目从一个国家切换到另一个城市,但我不能回到原来的(国家)一个。我怎么能这么做?
我的HTML:
<body ng-controller="test">
<div ng-repeat="module in values" ng-click="select? setSelected() : setSelected2();select = !select">
{{module.country}}
</div>我的.js文件:
var app = angular.module('AngularApp', []);
app.controller('test', ['$scope', function ($scope) {
$scope.values = [
{
country: 'England',
city: 'London',
},
{
country: 'Spain',
city: 'Madrid',
}];
$scope.select = true;
$scope.setSelected = function() {
$scope.values[this.$index].country = $scope.values[this.$index].city;
}
$scope.setSelected2 = function() {
$scope.values[this.$index].country = $scope.values[this.$index].country;
}
return false;
}]);下面是我的代码的柱塞:https://plnkr.co/edit/MMqDXJFE9dUcWvuGKRDV?p=preview
谢谢你的帮助。
发布于 2016-01-14 12:49:17
如果只用于显示目的,这可以帮助您,而不需要将任何角函数放置在.js文件中:
<div ng-repeat="module in values" ng-click="select = !select">
{{select ? module.city : module.country}}
</div>发布于 2016-01-14 12:51:02
您应该创建自己的模型来存储显示的值:
$scope.values = [
{
country: 'England',
city: 'London',
showCountry: true
},
{
country: 'Spain',
city: 'Madrid',
showCountry: true
}];
$scope.setSelected = function() {
if ($scope.values[this.$index].showCountry)
{
$scope.values[this.$index].showCountry = false;
} else {
$scope.values[this.$index].showCountry = true;
}
}
<div ng-repeat="module in values" ng-click="setSelected()">
{{module.showCountry ? module.country : module.city}}
</div>https://stackoverflow.com/questions/34789898
复制相似问题