Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ng单击更改JSON调用_Javascript_Json_Angularjs - Fatal编程技术网

Javascript 使用ng单击更改JSON调用

Javascript 使用ng单击更改JSON调用,javascript,json,angularjs,Javascript,Json,Angularjs,我有以下问题,我就是找不到解决办法 这是我当前用于Angular的JS文件: (function() { var app = angular.module("dota2impetus", [ ]); app.controller("heroGuide", ["$scope", "$http", function($scope, $http) { $scope.heroes = [ ]; $http.get("js/custom/heroes

我有以下问题,我就是找不到解决办法

这是我当前用于Angular的JS文件:

(function() {

    var app = angular.module("dota2impetus", [ ]);

    app.controller("heroGuide", ["$scope", "$http", function($scope, $http) {

        $scope.heroes = [ ];

        $http.get("js/custom/heroes.json").success(function(data) {
            $scope.heroes = data;
        });

    }]);

})();
JSON:

我可以通过以下调用从JSON获取数据:

{{ heroes.cw[0].name }}
{{ heroes.cw[0].type }}
{{ heroes.cw[0].roles }}
我需要使用一个变量,这样我就可以用ng click的缩写替换cw

<div title="Clockwerk" ng-click="selectHero(heroes.cw[0].abbr)"></div>
它现在应该在{{name}中显示Clockwerk,但它不会工作。我有什么遗漏吗


基本上,我想保留{{heros.x[0].name},x应该替换为我在ng click中从函数中得到的值(在本例中为“cw”)。

使用
[]
对象表示法,它允许在大括号之间传递变量

$scope.name = heroes[selectedHero][0].name;

嗯,我现在觉得有点傻。谢谢,很好用!
$scope.selectHero = function(selectedHero) {
    $scope.name = heroes.selectedHero[0].name;
};
$scope.name = heroes[selectedHero][0].name;