Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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 使用angularjs从json文件夹检索数据_Javascript_Html_Json_Angularjs - Fatal编程技术网

Javascript 使用angularjs从json文件夹检索数据

Javascript 使用angularjs从json文件夹检索数据,javascript,html,json,angularjs,Javascript,Html,Json,Angularjs,您好,我尝试创建一个angularjs应用程序,因此我从json文件夹中检索数据,但它的显示方式如下[“冒险”,“科幻”] 我如何才能从这个[“冒险”,“科幻”]中删除[“”] 这是我的json文件夹 [ { "title": "Interstellar", "genre": [ "adventure", "sci-fi" ], "watched": false }, { "tit

您好,我尝试创建一个angularjs应用程序,因此我从json文件夹中检索数据,但它的显示方式如下[“冒险”,“科幻”] 我如何才能从这个[“冒险”,“科幻”]中删除[“”]

这是我的json文件夹

[
    {
      "title": "Interstellar",
      "genre": [
        "adventure",
        "sci-fi"
      ],
      "watched": false
    },
    {
      "title": "Inception",
      "genre": [
        "action",
        "mystery",
        "sci-fi"
      ],
      "watched": true
    }
]
这是我的service.js

var app = angular.module('appMovies', [] );


app.service('moviesService', function($http,$q){

    var deferred =$q.defer();
    $http.get('movies.json').then(function (data)
    {
        deferred.resolve(data);

        });

    this.getPlayers = function ()
    {
        return deferred.promise;
    }
})
这是我的控制器

app.controller('appcontrolles', function($scope,moviesService){
    var promise = moviesService.getPlayers();
    promise.then(function(data)
{
    $scope.players =data.data;
    console.log($scope.players);
});
})
这是我的index.html

    <table class="table table-striped">
        <thead>

            <tr>

                <th>title</th>
                <th>genre</th>
                <th>watched</th>
            </tr>

        </thead>
        <tbody>
            <tr ng-repeat="movie in players | filter : genre |filter: search.genre |filter : watched ">

                <td>{{movie.title}}</td>
                <td>{{movie.genre}}</td>
                <td><input type="checkbox" name="vu", ng-model="movie.watched",value="true"></td>
            </tr>
        </tbody>
    </table>

标题
体裁
注视
{{movie.title}
{{电影类型}

感谢您的帮助

因为
movie.genre
是一个数组,当您刚刚放置
{{movie.genre}
时,Angular会将值作为表示数组的字符串输出:
[“冒险”,“科幻”]

如果需要一个简单的逗号分隔的值列表,可以使用数组的
.join()
函数创建一个带有指定分隔符的字符串,如
,“
,如:

<td>{{movie.genre.join(", ")}}</td>
离题:

您不需要对已经返回承诺的内容使用
$q.defer
,例如
$http
——您只需返回该承诺即可,因此您的服务可以简化为:

app.service('moviesService', function($http){
    this.getPlayers = function()
    {
        return $http.get('movies.json');
    }
});
$q.defer
用于转换某些第三方服务的非承诺异步函数,例如,成功时使用
出错时使用
处理程序

app.service('moviesService', function($http){
    this.getPlayers = function()
    {
        return $http.get('movies.json');
    }
});