角度服务无法正常工作

时间:2015-01-08 18:27:45

标签: javascript angularjs node.js

我正在使用Angular(客户端)和Node(服务器端)构建应用程序。我在Angular中使用了一些API,但我仍然坚持从特定的API中检索数据。有人可以帮忙吗?

这是我的API的回复JSON:

{
    "devices": [
        {
            "name": "led1",
            "driver": "Led",
            "connection": "driver",
            "commands": [
                "is_on",
                "turn_on",
                "turn_off",
                "toggle",
                "brightness",
                "current_brightness"
            ],
            "details": {
                "pin": 12
            }
        },
        {
            "name": "led2",
            "driver": "Led",
            "connection": "driver",
            "commands": [
                "is_on",
                "turn_on",
                "turn_off",
                "toggle",
                "brightness",
                "current_brightness"
            ],
            "details": {
                "pin": 13
            }
        },
        {
            "name": "sensor",
            "driver": "AnalogSensor",
            "connection": "driver",
            "commands": [
                "analog_read"
            ],
            "details": {
                "pin": 0
            }
        }
    ]
}

这是我的角色服务:

angular.module('robots.services', ['ngResource'])
.factory('Robots', function($resource) {
  return $resource(
      'http://dionaudio.local:8090/api/robots/Dionaudio.local/devices/:devices',
      { //method: 'robots', q: 'robots'
      }, // Query parameters
      {'query': { method: 'GET' }}
);
});

Controller and app.js

.controller('RobotsCtrl', function($scope, Robots) {
  $scope.robots = Robots.query();
});

    .config(function($stateProvider, $urlRouterProvider) {
      $stateProvider

      .state('app', {
        url: "/app",
        abstract: true,
        templateUrl: "templates/menu.html",
        controller: 'AppCtrl'
      })

      .state('app.robots', {
        url: "/robots",
        views: {
          'menuContent': {
            templateUrl: "templates/robots.html",
            controller: 'RobotsCtrl'
          }
        }
      })

      .state('app.deviceinfo', {
        url: "/deviceinfo",
        views: {
          'menuContent': {
            templateUrl: "templates/deviceinfo.html",
            controller: 'DeviceinfoCtrl'
          }
        }
      });

      // if none of the above states are matched, use this as the fallback
      $urlRouterProvider.otherwise('/app/deviceinfo');
    });

最后我的模板:

<ion-item ng-repeat="robot in robots" href="">{{robot.name}}</ion-item>

我只获得一个结果而没有名称输出。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我认为你需要指向json中的数组

<ion-item ng-repeat="robot in robots.devices" href="">{{robot.name}}</ion-item>

对于下面的问题,你需要像

这样的东西
  .state('app.robots', {
    url: "/robots/:robotName",
    views: {...
相关问题