将参数传递给温泉服务
问题描述:
我明显缺少基本onsen/angular的东西,但我不清楚如何将参数传递到查询数据库的服务中。这一切都很好,当我硬编码查询,而不是使用“身份证”参数:将参数传递给温泉服务
.factory('Inscription', function(DB) {
var self = this;
self.all = function(id) {
return DB.query('SELECT * FROM inscription where project_id = ? limit 100', [3])
.then(function(result){
return DB.fetchAll(result);
});
};
,但我不知道如何id参数动态传递给该查询。相关的控制器代码:
.controller('InscriptionsCtrl', function($scope, Inscription) {
$scope.inscriptions = [];
$scope.inscription = null;
// Get all the project inscriptions
Inscription.all().then(function(inscriptions) {
$scope.inscriptions = inscriptions;
});
其在列表中使用:
<ons-template id="showProject.html">
<ons-page>
<ons-list ng-controller="InscriptionsCtrl">
<ons-list-item ng-repeat="inscription in inscriptions" ng-click="navi.pushPage('showInscription.html')">
{{inscription.title}} - {{inscription.description | limitTo:50}}...
</ons-list-item>
</ons-list>
</ons-page>
</ons-template>
与列表页面被传递项目ID在数据库查询中使用:
$ scope.ons.navigator.pushPage('showProject.html',{project_id:id});
我只是不知道如何将该ID传递给控制器以形成正确的查询。
答
推送页面时使用的参数将被绑定到页面对象。
在推页面的控制器,你可以这样做:
var id = myNavigator.getCurrentPage().options.project_id;
Inscription.all(id).then(function(result) ...
精彩,感谢安德烈亚斯。 – Richard 2014-11-25 09:30:38