角度自定义过滤器,里面有承诺

时间:2015-08-05 13:10:13

标签: javascript angularjs filter angular-promise

我想要实现的是使用过滤器,它将从ret()函数返回成功或错误。使用下面的代码,它返回{},这可能是它的承诺。

.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;
        });
    }
}]);

此过滤器仅用于一个ng-repeat,因此我可以将函数绑定到ng-repeat,如:

HTML

{{getPostName(item.id)}}

Angular.js

function getPostName(id) {
    return post[id].name;
}

1 个答案:

答案 0 :(得分:3)

根据您的评论

  

我在DB中获取了PostCode的ID需要获取它的值并代替id = 1的ID,然后值为00-000

您需要使用指令来调用数据库并执行DOM操作。

http://www.sitepoint.com/practical-guide-angularjs-directives/

<强>指令:

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>

<强> 编辑:

由于此特定指令与其父级共享作用域,因此您只需编辑模板以使用传入的任何内容,在这种情况下为i

<强> HTML

<tr ng-repeat="i in data.contacts">
    <td>
        <div data-postcode></div>
    </td>
</tr>

<强>指令

template: '{{getPostName(i.id)}}'