【问题标题】:Trouble accesing JSON file with angular无法使用角度访问 JSON 文件
【发布时间】:2016-10-05 18:02:02
【问题描述】:

这是我使用的 JSON 和控制器:

JSON 格式:

{"tomcatLogDir":"C:\\apache-tomcat-7.0.70\\logs","tomcatLogs":["1.log","2.log","3.log"]}

工厂:

app.factory('filesFactory', [ '$http', 
                                        function($http) {
                                            return $http.get("http://"+ boxIP +":"+ boxPort +"/monitorTool/rest/toolservice/files")
                                                .success(function(data) {
                                                    return data;
                                                }).error(function(err) {
                                                    return err;
                                                })
                                            } ]);

控制器:

app.controller('tomcatLogDirController', [ '$scope', 'filesFactory',
                                            function($scope, jsonFactory) {
                                                jsonFactory.success(function(data) {
                                                        $scope.tomcatDir = data;
                                        });
                                    }]);

HTML:

    <div ng-controller="tomcatLogDirController">
        <ul>
            <li ng-repeat="x in tomcatDir">
                <a ng-href="{{'file?name=' + tomcatDir.tomcatLogDir + x.tomcatLogs}}">{{x.tomcatLogs}}</a>
            </li>
        </ul>
    </div>

所以我尝试了这个,tomcatLogs 没有出现,它们似乎没有被加载到 html 中。

我做错了什么,我做错了对 JSON 文件的调用?

谢谢。

【问题讨论】:

    标签: html angularjs json angularjs-ng-repeat angularjs-controller


    【解决方案1】:

    检查你的控制器,你应该有'filesFactory'而不是jsonFactory

    应该有正确的参数,

    app.controller('tomcatLogDirController', [ '$scope', 'filesFactory',
                                                function($scope, filesFactory) {
    

    编辑

      <ul>
       <li ng-repeat="x in tomcatDir.tomcatLogs">
         <a >{{x}}</a>
       </li>
      </ul>
    

    DEMO

    【讨论】:

    • 如何在 ng-repeat 中调用 tomcatLogs?
    • 检查样本\
    • 谢谢,现在如果我想正确连接ng-href="{{'file?name=' + tomcatDir.tomcatLogDir + x.tomcatLogs}}",因为它不会在链接末尾显示\1.log
    • 再次感谢您的智慧! :)
    猜你喜欢
    • 2021-04-29
    • 2012-05-21
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    相关资源
    最近更新 更多