【发布时间】:2015-10-05 05:55:54
【问题描述】:
我是 Angular 新手,我想知道如何在 ng-click 上更改 json 数组。我尝试了不同的方法,但代码不起作用。我不确定如何创建此功能。
这是我的代码。我希望在应用加载时默认为第 1 章,当用户单击菜单链接时,它会更改为传递给函数的任何章节号。
menu.html ---- 视图
<li>
<a href>Practice</a>
<ul>
<li ng-repeat="words in allTerms track by $index">
<a href class="{{$index}}" id="{{ $index }}" ng-click="testing($event); PracticeTerm(); changeData('chapter2');" >{{ words.term }}</a>
</li>
</ul>
</li>
我正在使用服务加载数据
app.factory('quest', ['$http', function($http) {
return $http({
method: 'GET',
url: 'data/study.json'
}).success(function(data) {
return data;
})
.error(function(err) {
return err;
});
}]);
这是迄今为止我的控制器中的代码 sn-p。
quest.success(function(data) {
$scope.data = data;
$scope.total = data.chapter1.length;
$scope.allTerms = data.chapter1;
// stores current term index
$scope.shared = [];
$scope.changeData = function(thechapter){
return $scope.total = 'data.'+ thechapter + '.length';
$scope.allTerms = 'data.' + thechapter;
console.log($scope.total);
};
});
JSON 数据----示例数据
"chapter1": [
{
"id": "1",
"chapter": "1",
"term": "Closingstage",
"pronun": "kloh-zing steyj",
"soundfile": "",
"definition": "Closing stage definitions ---------CLOSING STAGE DEFINITION"
},
{
"id": "2",
"chapter": "1",
"term": "CaptialWords",
"pronun": "CAP-I-Tal Worlds",
"soundfile": "",
"definition": "Capital Worlds definitions----------------CAPITAL WORLD DEFINITION"
}
],
"chapter2": [
{
"id": "1",
"chapter": "2",
"term": "Closingstage2222",
"pronun": "kloh-zing steyj222222222",
"soundfile": "",
"definition": "Closing stage definitions ---------CLOSING STAGE DEFINITION22222222"
},
{
"id": "2",
"chapter": "2",
"term": "CaptialWords22222222222",
"pronun": "CAP-I-Tal Worlds",
"soundfile": "",
"definition": "Capital Worlds definitions----------------CAPITAL WORLD DEFINITION222222222222"
}
]
【问题讨论】:
-
“它会改变”是什么意思?有什么变化?
-
我的意思是章节中的数据发生了变化。例如,如果用户单击第 2 章,则第 2 章数组中的数据将在调用时显示。
标签: javascript jquery json angularjs model-view-controller