【问题标题】:Editing JSON search results from within Angular从 Angular 中编辑 JSON 搜索结果
【发布时间】:2016-12-16 12:19:09
【问题描述】:

我现在将数据从外部 JSON URL 正确提取到母版页,但我的详细信息页面似乎没有传递从 http.get 接收到的对象 initallt。该应用程序的主要部分可以在代码笔中查看CODEPEN

<label class="item item-input">
<i class="icon ion-search placeholder-icon"></i>
<input type="search" ng-model="query" placeholder="Search Slugname">
<button class="button button-dark" ng-click="getOrders()">Submit</button>
</label>

如果我的用户想要手动更改日期(order.date)值以说“10/8/16”。 如何访问/编辑从外部 API 返回的任何 JSON 值?

我最终希望在我的应用程序中编辑返回的 JSON 数据,然后将修改后的数据发布回 PHP API 服务器。

【问题讨论】:

    标签: javascript angularjs json stringify


    【解决方案1】:

    https://codepen.io/anon/pen/qNLOAP

    显然,您似乎不能在带有ng-click&lt;label&gt; 中拥有一个按钮。我将标签更改为&lt;div&gt;,它开始工作了。

    搜索目前不起作用,但应该可以让您继续。

    【讨论】:

    • 感谢 Daniel,test-3 现在返回数据 - 我有可能允许这样做的 CORS Chrome 插件。只需要知道如何访问/编辑“日期”的 JSON 值。也许需要添加 JSON 字符串化或解析,不确定...
    【解决方案2】:

    请解决列出的问题。

    1> tabs.home 状态你没有注入控制器,另一件事也是定义 home.html 和

    2> 与控制器平行的模板文件夹中的所有其他模板。

    3> http 调用 http://mvcframe.com/wpapi/wp-json/wp/v2/pages 需要跨域支持,您可以使用 chrome (CORS) 插件。

    它会为我工作。请检查并告诉我它是否有效。

    【讨论】:

    • 忽略选项卡,这只是我从其中运行 Ionic 的 Codepen 的设置。我在浏览器中运行了 CORS 插件,请提供您正在使用的版本的工作分叉 CodePen。
    【解决方案3】:

    您的主要问题是您想修改来自 $http 调用的传入数据。

    你可以实现一个http拦截器,方法response会接受response修改它然后返回给调用者。由于 http 拦截器将接收所有传入的请求,因此只需检查 url start 以不修改其他请求。

    angular.module('ionicApp', ['ionic'])
    .factory('httpInterceptor', function($q, $rootScope) {
        var httpInterceptor = {
            response: function(response) {
                var deferred = $q.defer();
                var results = response.data;
                var urlStart = 'http://mvcframe.com/wpapi/wp-json/wp/v2/pages?slug=';
                if (response.config.url.startsWith(urlStart)) {
                    angular.forEach(results, function(result, key) { 
                        result.date = '10/8/16'; 
                    });
                }
                deferred.resolve(response);
                return deferred.promise;
            }
        };
        return httpInterceptor;
    })
    .config(function($httpProvider) { 
        $httpProvider.interceptors.push('httpInterceptor'); 
    })
    .controller('ListController', 
        ['$scope', '$http', '$state', '$stateParams', '$window', '$location', 
        function($scope, $http, $state, $stateParams, $window, $location) {
            $scope.getOrders = function(query) {
                $http.get('http://mvcframe.com/wpapi/wp-json/wp/v2/pages?slug=' + query)
                .success(function(data) {
                    $scope.orders = data;
                })
            }
            $scope.orders = [];
    }]);
    

    我对您的 html 所做的唯一修改是将查询参数直接发送到 ng-click 上的函数

    <html ng-app="ionicApp">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
        <title>Tabs Example</title>
        <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
        <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
    </head>
    <body ng-controller="ListController">
        <ion-header-bar class="bar-dark" align-title="center">
            <h2 class="title">Order Search</h2>
        </ion-header-bar>
        <ion-content padding="true" class="has-header">
            <div class="item item-input">
                <i class="icon ion-search placeholder-icon"></i>
                <input type="text" ng-model="query" placeholder="Search Slugname">
                <button type="submit" class="button button-dark" ng-click="getOrders(query)">Submit</button>
            </div>
            <p>An example and working slug search term is test-3. The JSON can be viewable in a browser using https://mvcframe.com/wpapi/wp-json/wp/v2/pages?slug=test-3</p>
            <ion-list>
                <ion-item ng-repeat="order in orders | filter:query" class="item-thumbnail-left item-text-wrap" href="#/tab/list/{{order.id}}">
                    <h2>Page ID: {{order.id}}</h2>
                    <h3>Date created: {{order.date}}</h3>
                    <h2>Page URL: £{{order.link}}</h2>
                    <h2>Page Title: £{{order.title/rendered}}</h2>
                </ion-item>
            </ion-list>
        </ion-content>
    </body>
    </html>
    

    我差点忘了codepen在这里:http://codepen.io/pachon1992/pen/QEodJR

    编辑---------------

    根据 cmets,您希望您的用户手动更新数据,对吗?例如,您想更新日期,您可以为用户启用输入以编辑数据,因为 Angular 是双向数据绑定会修改您的数据。

    <html ng-app="ionicApp">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
        <title>Tabs Example</title>
        <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
        <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
    </head>
    <body ng-controller="ListController">
        <ion-header-bar class="bar-dark" align-title="center">
            <h2 class="title">Order Search</h2>
        </ion-header-bar>
        <ion-content padding="true" class="has-header">
            <div class="item item-input">
                <i class="icon ion-search placeholder-icon"></i>
                <input type="text" ng-model="query" placeholder="Search Slugname">
                <button type="submit" class="button button-dark" ng-click="getOrders(query)">Submit</button>
            </div>
            <p>An example and working slug search term is test-3. The JSON can be viewable in a browser using https://mvcframe.com/wpapi/wp-json/wp/v2/pages?slug=test-3</p>
            <ion-list>
                <ion-item ng-repeat="order in orders | filter:query" class="item-thumbnail-left item-text-wrap" href="#/tab/list/{{order.id}}">
                    <h2>Page ID: {{order.id}}</h2>
                    <div><input type="text" ng-model="order.date"></div>
                    <h2>Page URL: £{{order.link}}</h2>
                    <h2>Page Title: £{{order.title/rendered}}</h2>
                </ion-item>
                <button type="button" class="button button-dark" ng-click="update()">Update</button>
            </ion-list>
        </ion-content>
    </body>
    </html>
    

    在您的控制器中,您可以为每个订单调用通常通过 PUT 端点调用的 http 服务

    angular.module('ionicApp', ['ionic'])
    
    .controller('ListController', ['$scope', '$http', '$state', '$stateParams', '$window', '$location',
        function($scope, $http, $state, $stateParams, $window, $location) {
            $scope.query = "";
            $scope.getOrders = function(query) {
                $http.get('http://mvcframe.com/wpapi/wp-json/wp/v2/pages?slug=' + query)
                    .success(function(data) {
                        $scope.orders = data;
                    });
            }
            $scope.orders = [];
            $scope.update = function() {
                angular.forEach($scope.orders, function(order) {
                    //whetever url you are using
                    $http.put('http://mvcframe.com/wpapi/wp-json/wp/v2/pages/update/' + order.id, order, {})
                        .success(function(data) {
                            console.log('updated');
                        });
                });
            };
        }
    ]);
    

    我已经编辑了代码笔

    【讨论】:

    • 以前没见过$q.defer,这符合这家伙说的吗? codelord.net/2015/09/24/$q-dot-defer-youre-doing-it-wrong data.order.date = "new value" 会是另一种可行的选择吗?
    • 这看起来非常好,谢谢。理想情况下,我想首先使用 test-3 加载数据。然后单击数据下方的按钮(例如),然后将日期更改为当前日期,例如var date = new Date(); date.getDate() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getFullYear()).slice(-2);
    • 这个方法不是原帖要求的。 $http 拦截器将在收到数据后立即自动更新日期字段。原始帖子要求访问数据以允许用户在以后的交互中手动更新数据(日期字段是一个示例)。
    • 将“查询”参数显式传递给 getOrders() 函数的方法可能是为您解决此问题的方法。在这种情况下,它永远不会在控制器 $scope 上设置查询字段,而是在子范围上设置,然后将其传递给函数。我更喜欢 $parent.query 因为你知道你可以在控制器的任何地方访问 $scope.query。
    • 你是对的@fourfightingfoxes,直到现在我的回复中还没有任何 cmets
    【解决方案4】:

    工作笔: https://codepen.io/jasondecamp/pen/gryxGQ

    我所做的两个更改是: 1) 将 $parent 添加到输入字段 ng-model 中对“查询”的引用。我对 ionic 不太熟悉,但我的猜测是 ion-content 指令添加了一个子范围,因此要引用查询,您需要查看父级。

    <input type="search" ng-model="$parent.query" placeholder="Search Slugname">

    2) 我将 http get url 更改为 https,因为我收到了不安全的访问警告。

    需要注意的是,当响应为纯 JSON 数据时,不需要使用任何 JSON 解析函数,因为 Angular $http 服务会自动识别数据并将其转换为 js 对象。

    【讨论】:

      猜你喜欢
      • 2016-06-11
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多