【问题标题】:Angular service not working properlyAngular 服务无法正常工作
【发布时间】:2015-03-06 23:26:39
【问题描述】:

我正在使用 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' }}
);
});

控制器和 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>

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

【问题讨论】:

  • 不,没有控制器。
  • Name 是设备数组中 JSON 对象的属性。你想要{{robots.devices.name}}
  • 你应该有一个控制器。请显示代码。
  • 对不起,我正在编辑附加信息 :-) 它现在就在那里!

标签: javascript angularjs node.js


【解决方案1】:

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

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

对于您下面的问题,您需要类似

  .state('app.robots', {
    url: "/robots/:robotName",
    views: {...

【讨论】:

  • 希望我没有问太多,但是 Angular 是相当困难的。现在我无法让细节部分起作用。这是我的控制器:code .controller('RobotCtrl', function($scope, $stateParams, Robot) { $scope.robot = Robot.get({robotName: $stateParams.robotName}); });
  • 再次感谢伙计!不幸的是,我没有提供帮助,我现在在主列表中显示详细信息...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-08
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
相关资源
最近更新 更多