在ng-click上更改json调用

时间:2015-07-15 23:51:46

标签: javascript jquery json angularjs model-view-controller

我是角色的新手,我想知道如何在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; 
     });
    }]); 

以下是我到目前为止控制器的代码段。

    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"
    }
]

1 个答案:

答案 0 :(得分:0)

尝试使用以下功能代替控制器中的功能

$scope.changeData = function(thechapter){       
 $scope.allTerms = data[thechapter];
 console.log($scope.total);
 return $scope.total = data[thechapter].length;
 };

请注意,我已将返回行移动到该函数的最后一行,以便其他行运行。

相关问题