Angular 服务无法正常工作
Angular service not working properly
我正在使用 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 服务:
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>
我只得到一个结果,没有名字输出。我做错了什么?
我认为您需要指向 json
中的数组
<ion-item ng-repeat="robot in robots.devices" href="">{{robot.name}}</ion-item>
对于下面的问题,您需要
.state('app.robots', {
url: "/robots/:robotName",
views: {...
我正在使用 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 服务:
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>
我只得到一个结果,没有名字输出。我做错了什么?
我认为您需要指向 json
中的数组<ion-item ng-repeat="robot in robots.devices" href="">{{robot.name}}</ion-item>
对于下面的问题,您需要
.state('app.robots', {
url: "/robots/:robotName",
views: {...