Angular custom filter with promise inside

ぐ巨炮叔叔 提交于 2019-12-24 13:25:02

问题


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;
}

回答1:


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)}}'


来源:https://stackoverflow.com/questions/31833536/angular-custom-filter-with-promise-inside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!