简体   繁体   English

Angular JS提取JSON数据

[英]Angular JS extracting JSON data

I'm trying to learn Angular JS to create web applications. 我正在尝试学习Angular JS来创建Web应用程序。 I've gone through Angular JS's phonecatApp tutorial online, and am trying to adapt it to store some information about bases 我已经在线阅读Angular JS的phonecatApp教程 ,并试图对其进行修改以存储一些有关基础的信息

I currently have the index.html file that calls the ngview directive. 我目前有一个调用ngview指令的index.html文件。 I've split the html pages into two templates and two respective controllers. 我将html页面分为两个模板和两个相应的控制器。

I'm also using a custom service to get information from a JSON file that has the data I'm working with. 我还使用自定义服务从包含我正在使用的数据的JSON文件中获取信息。

Here is my code "app.js" that invokes the respective pages and templates: 这是我的代码“ app.js”,它调用相应的页面和模板:

'use strict';

/* App Module */

var bracApp = angular.module('bracApp', [
  'ngRoute',
  'bracControllers',
  'bracServices',
  'myDropdown'
  /* creates above as dependencies of the application module */
]);

bracApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/base', {
        templateUrl: 'partials/brac-list.html',
        controller: 'bracListCtrl'
      }).
      when('/base/:baseId', {
        //* everything with ":" beforehand is extracted into the $routeParams object *//
        templateUrl: 'partials/brac-detail.html',
        controller: 'bracDetailCtrl'
      }).
      otherwise({
        redirectTo: '/base'
      });
  }]);

Here is the "controllers.js" file: 这是“ controllers.js”文件:

'use strict';

/* Controllers */

var bracControllers = angular.module('bracControllers', [])

bracControllers.controller('bracListCtrl', ['$scope', 'Base', function($scope, Base) {
  $scope.bases = Base.query();
  $scope.orderProp = 'Bases';
}]);

bracControllers.controller('bracDetailCtrl', ['$scope', '$routeParams', 'Base', function($scope,
$routeParams, Base) {
  $scope.base = Base.get({baseId: $routeParams.baseId});
}]);

Here is the "services.js" file which should fetch the JSON information from bases.json: 这是“ services.js”文件,应从bases.json获取JSON信息:

'use strict';

/* Services - used to link data to application from JSON*/

var bracServices = angular.module('bracServices', ['ngResource']);

bracServices.factory('Base', ['$resource',
  function($resource) {
    return $resource('bases.json', {}, {
      query: {method: 'GET', params:{baseId:'Bases'}, isArray:true}
    });
  }]);

The JSON file has a several objects that consist of only two properties: "Bases" and "Information". JSON文件具有几个仅由两个属性组成的对象:“基础”和“信息”。

Edit: 编辑:

The JSON data is in the following format: JSON数据采用以下格式:

[{"Bases":"Base Location 1","Information":"Minimal "},
{"Bases":"Base Location 2","Information":"Minimal "},
{"Bases":"Base Location 3","Information":"Minimal "},
{"Bases":"Base Location 4","Information":"Minimal "}]

I substituted the locations of the bases, they are strings that include letters, ",", and ".". 我替换了基址的位置,它们是包含字母,“,”和“。”的字符串。

What I am trying to do is simply display the value of the "Bases" property in the brac-detail.html of the selected base in the dropdown menu (posted below). 我想做的只是在下拉菜单(在下面发布)中,在所选基准的brac-detail.html中显示“基准”属性的值。 Basically, I'm trying to put the selected Base from the list (of the property "Bases" in the JSON data) as a header, so something like <h3>{{currentBase}}</h3> . 基本上,我试图将列表中(JSON数据中的“ Bases”属性的列表)中选定的Base作为标题,例如<h3>{{currentBase}}</h3> I'm not sure how to refer to the current, picked base from the dropdown menu. 我不确定如何从下拉菜单中引用当前选择的基准。 Can anyone shed some light on how this is done? 谁能阐明这是如何完成的? Thanks and please let me know if you'd like me to clarify anything. 谢谢,如果您想让我澄清一下,请告诉我。

Here is the code for the main page ( working ) 这是主页的代码( 工作

<div id="background">
  <div id="pagebackground">

<div id="table1">

  <div id="BracDropdown" class="btn-group" dropdown is-open="status.isopen">
    <button type="button" class="btn btn-primary dropdown-toggle" ng-disabled="disabled">
      BRAC Bases <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" role="menu">
      <li ng-repeat="base in bases | orderBy: orderProp">
        <a href="#/base/{{base.Bases}}">{{base.Bases}}</a>
      </li>
    </ul>
  </div>

  <div id="selector">
    <p id="sortby"> Sort by:</p>
    <select ng-model="orderProp">
      <option value="Bases">Name</option>
      <option value="Information">Information</option>
    </select>
  </div>

</div>

I'm getting the following errors when I run the application in Firefox: "not well-formed" - brac-list.html, line 24 "syntax error" - bases.json, line 1 在Firefox中运行应用程序时出现以下错误:“格式不正确”-brac-list.html,第24行“语法错误”-bases.json,第1行

and when I click a menu item from the dropdown, I get the following: "Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an object but got an array" 当我从下拉菜单中单击一个菜单项时,我得到以下信息:“错误:[$ resource:badcfg]资源配置错误。预期响应包含一个对象但得到一个数组”

Good going, I would suggest to use promises while returning data from service to any controller. 很好,我建议在将数据从服务返回到任何控制器时使用Promise。 like this example: Example 如下例所示: 实施例

And assign your array to a variable which will be accessible from view after the call. 并将您的数组分配给一个变量,该变量将在调用后从视图中访问。 Let me know if you have any other query. 让我知道您是否还有其他查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM