【发布时间】:2016-10-30 19:46:16
【问题描述】:
我正在尝试通过 2 个不同的 json 文件加载和附加 2 个结果。 我的 Html 文件如下所示
<ion-view view-title="Dashboard" class="has-subheader">
<ion-content scroll="false">
<div class="row" style="padding-left:55px;">
<div class="col col-60" style="border-radius: 20px; height:90%;border: 2px solid #73AD21;min-height:240px;">
<div class="form-group" ng-repeat="input in inputs" id="content">
<div ng-switch="input.type">
<div ng-switch-when="text" >
<label class="">{{input.name}}</label>
<input type="text" value="{{input.value}}"/>
</div>
<div ng-switch-when="checkbox">
<input type="checkbox" value="outputs[input.name]"/ >
</div>
<div ng-switch-when="button" class="btn-group inline pull-left">
<input ID="{{input.id}}" type="submit" value="{{input.value}}" class="{{input.class}}">
</div>
</div>
</div>
</div>
<div class="col col-40" style="border-radius: 20px; height:90%;border: 2px solid #73AD21;">
<a ng-click="loadPeople($event)" class="pointer" id="1">Link 1</a><br>
<a ng-click="loadPeople($event)" class="pointer" id="2">Link 2</a>
</div>
</div>
</ion-content>
</ion-view>
控制器
angular.module('starter.controllers', [])
.controller('ChatsCtrl',function($scope,$http){
$scope.loadPeople = function(event) {
$scope.outputs = {};
$scope.inputs = {};
if(event.target.id==1)
{
var object1= $http.get('js/link1.json').success(function(response){
$scope.inputs=response;
});
}
else if(event.target.id==2){
var object2=$http.get('js/link2.json').success(function(response){
$scope.inputs=response;
});
}
};
})
link1.json
[{
"type": "text",
"value":"First",
"name": "first",
"required":"true"
}, {
"type":"button",
"value":"First",
"id":"first",
"class":"btn-primary"
},{
"type":"button",
"value":"Second",
"id":"second",
"class":"btn-danger"
},{
"type":"button",
"value":"Third",
"id":"Third",
"class":"btn-assertive"
}]
link2.json
[{
"type": "text",
"name": "first"
},
{
"type": "text",
"name": "fifth"
}, {
"type": "checkbox",
"name": "third"
},{
"type": "text",
"name": "second"
} ]
当我单击它时,我想附加并显示来自 link2.json 的数据。请帮助我或提供有用的链接,我是 angularjs 的新手。感谢期待
【问题讨论】:
-
$scope.inputs = $scope.inputs.concat(response);。您可能还应该将输入声明为数组:$scope.inputs = [];. -
将申请并尽快通知您
-
我按照但未修改 $scope.inputs = []; if(event.target.id==1) { var object1= $http.get('js/link1.json').success(function(response){ $scope.inputs = $scope.inputs.concat(response) ; }); } else{ var object1=$http.get('js/text.json').success(function(response){ $scope.inputs = $scope.inputs.concat(response); }); } };
标签: javascript html angularjs json append