与诺里角自定义过滤器 [英] Angular custom filter with promise inside
问题描述
我试图使用过滤器,将返回成功或错误从 RET()
函数来实现。有了它下面的code返回 {}
,这可能是它的承诺。
.filter('后code',['$ cordovaSQLite','$ Q',
功能($ cordovaSQLite,$ Q){
返回功能(邮政codeID){
功能RET(){
变种高清= $ q.defer();
ionic.Platform.ready(函数(){
如果(window.cordova){
变种DB = $ cordovaSQLite.openDB({
名称:msddocapp.db
});
}其他{
变种分贝= window.openDatabase(msddocapp.db,1,ES数据库,5 * 1024 * 1024);
}
VAR的查询=SELECT * FROM邮报code其中服务器ID =?;
$ cordovaSQLite.execute(DB,查询,[邮报codeID])。然后(功能(S){
如果(s.rows.length大于0){
def.resolve(s.rows.item(0).title伪);
}
},功能(E){
的console.log(E);
def.reject(邮政codeID);
})
});
返回def.promise;
}
返回RET()。然后(功能(多个){
返回S;
},功能(E){
返回e的;
});
}
}]);
这个过滤器只用于一个 NG-重复
,所以也许我可以绑定一个函数到NG-重复这样的:
HTML
{{getPostName(item.id)}}
Angular.js
函数getPostName(ID){
返回后[ID]。名称;
}
根据您的评论
我在ID后DB code的需要得到它的价值和到位像ID ID = 1那么值为00-000
的
块引用>您需要使用的指令,以使调用数据库,并执行DOM操作。
http://www.sitepoint.com/practical-guide-angularjs-directives /
指令:
angular.directive('后code',['$ cordovaSQLite','$ Q',函数($ cordovaSQLite,$ Q){
返回{
模板:{{getPostName(item.id)}}',
链接:功能(范围,ELEM,ATTRS){
scope.getPostName =功能(邮政codeID){
变种高清= $ q.defer();
ionic.Platform.ready(函数(){
如果(window.cordova){
变种DB = $ cordovaSQLite.openDB({
名称:msddocapp.db
});
}其他{
变种分贝= window.openDatabase(msddocapp.db,1,ES数据库,5 * 1024 * 1024);
}
VAR的查询=SELECT * FROM邮报code其中服务器ID =?;
$ cordovaSQLite.execute(DB,查询,[邮报codeID])。然后(功能(S){
如果(s.rows.length大于0){
def.resolve(s.rows.item(0).title伪);
}
},功能(E){
的console.log(E);
def.reject(邮政codeID);
})
});
返回def.promise.then(功能(S){
返回S;
},功能(E){
返回e的;
});
};
}
};
}]);HTML:
< DIV数据后code>< / DIV>
编辑:的
由于这个特殊的指令与其父共享的范围,你只需要编辑模板使用任何你传递,
I
在这种情况下:HTML
< TR NG重复=我data.contacts>
&所述; TD>
< DIV数据后code>< / DIV>
< / TD>
< / TR>指令
模板:{{getPostName(i.id)}}'
What I am trying to achieve is using a filter that will return success or error from the
ret()
function. With the code below it returns{}
, which is probably its promise..filter('postcode', ['$cordovaSQLite', '$q', function($cordovaSQLite, $q) { return function(PostCodeID) { function ret() { var def = $q.defer(); ionic.Platform.ready(function() { if (window.cordova) { var db = $cordovaSQLite.openDB({ name: "msddocapp.db" }); } else { var db = window.openDatabase("msddocapp.db", "1", "ES Database", 5 * 1024 * 1024); } var query = "select * from PostCode where ServerID = ?"; $cordovaSQLite.execute(db, query, [PostCodeID]).then(function(s) { if (s.rows.length > 0) { def.resolve(s.rows.item(0).Title); } }, function(e) { console.log(e); def.reject(PostCodeID); }) }); return def.promise; } return ret().then(function(s) { return s; }, function(e) { return e; }); } }]);
This filter is used for only one
ng-repeat
, so maybe I can bind a function to ng-repeat like:HTML
{{getPostName(item.id)}}
Angular.js
function getPostName(id) { return post[id].name; }
解决方案Based on your comment
I got ID of PostCode in DB need to Get Value of it and put in place of ID like id = 1 then value is 00-000
You need to use a directive in order to make the call to the database and perform the DOM manipulation.
http://www.sitepoint.com/practical-guide-angularjs-directives/
Directive:
angular.directive('postcode', ['$cordovaSQLite', '$q', function($cordovaSQLite, $q){ return { template: '{{getPostName(item.id)}}', link: function(scope, elem, attrs) { scope.getPostName = function(PostCodeID) { var def = $q.defer(); ionic.Platform.ready(function() { if (window.cordova) { var db = $cordovaSQLite.openDB({ name: "msddocapp.db" }); } else { var db = window.openDatabase("msddocapp.db", "1", "ES Database", 5 * 1024 * 1024); } var query = "select * from PostCode where ServerID = ?"; $cordovaSQLite.execute(db, query, [PostCodeID]).then(function(s) { if (s.rows.length > 0) { def.resolve(s.rows.item(0).Title); } }, function(e) { console.log(e); def.reject(PostCodeID); }) }); return def.promise.then(function(s) { return s; }, function(e) { return e; }); }; } }; }]);
HTML:
<div data-postcode></div>
EDIT:
Since this particular directive is sharing the scope with its parent you just need to edit the template to use whatever you are passing in,
i
in this case:HTML
<tr ng-repeat="i in data.contacts"> <td> <div data-postcode></div> </td> </tr>
Directive
template: '{{getPostName(i.id)}}'
这篇关于与诺里角自定义过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!