使用* ngFor(Angular2)显示Parse.com数据

时间:2016-02-15 16:48:05

标签: javascript parse-platform angular ionic2

抱歉,如果英语不好,我就是法语

我使用Ionic 2 Angular 2和Parse.com构建了一个应用程序。几天前,我正在使用Ionic 1并且我的代码没有问题。

当我使用Parse进行查询时,数据不会显示在我的HTML上,我不明白为什么。

这是我的JS:

 internal class QueryReflectionMethods
    {
        internal static System.Reflection.MethodInfo Enumerable_Select = typeof(Enumerable).GetMethods().First(x => x.Name == "Select" && x.GetParameters().Length == 2);
        internal static System.Reflection.MethodInfo Enumerable_DefaultIfEmpty = typeof(Enumerable).GetMethods().First(x => x.Name == "DefaultIfEmpty" && x.GetParameters().Length == 1);

        internal static System.Reflection.MethodInfo Queryable_SelectMany = typeof(Queryable).GetMethods().Where(x => x.Name == "SelectMany" && x.GetParameters().Length == 3).OrderBy(x => x.ToString().Length).First();
        internal static System.Reflection.MethodInfo Queryable_Where = typeof(Queryable).GetMethods().First(x => x.Name == "Where" && x.GetParameters().Length == 2);
        internal static System.Reflection.MethodInfo Queryable_GroupJoin = typeof(Queryable).GetMethods().First(x => x.Name == "GroupJoin" && x.GetParameters().Length == 5);
        internal static System.Reflection.MethodInfo Queryable_Join = typeof(Queryable).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).First(c => c.Name == "Join");
        internal static System.Reflection.MethodInfo Queryable_Select = typeof(Queryable).GetMethods().First(x => x.Name == "Select" && x.GetParameters().Length == 2);



        public static IQueryable<TResult> CreateLeftOuterJoin<TOuter, TInner, TKey, TResult>(
                   IQueryable<TOuter> outer,
                   IQueryable<TInner> inner,
                   Expression<Func<TOuter, TKey>> outerKeySelector,
                   Expression<Func<TInner, TKey>> innerKeySelector,
                   Expression<Func<TOuter, TInner, TResult>> resultSelector)
        { 

            var keyValuePairHolderWithGroup = typeof(KeyValuePairHolder<,>).MakeGenericType(
                typeof(TOuter),
                typeof(IEnumerable<>).MakeGenericType(
                    typeof(TInner)
                    )
                );
            var paramOuter = Expression.Parameter(typeof(TOuter));
            var paramInner = Expression.Parameter(typeof(IEnumerable<TInner>));
            var groupJoin =
                Queryable_GroupJoin.MakeGenericMethod(typeof(TOuter), typeof(TInner), typeof(TKey), keyValuePairHolderWithGroup)
                .Invoke(
                    "ThisArgumentIsIgnoredForStaticMethods",
                    new object[]{
                    outer,
                    inner,
                    outerKeySelector,
                    innerKeySelector,
                    Expression.Lambda(
                        Expression.MemberInit(
                            Expression.New(keyValuePairHolderWithGroup), 
                            Expression.Bind(
                                keyValuePairHolderWithGroup.GetMember("Item1").Single(),  
                                paramOuter
                                ), 
                            Expression.Bind(
                                keyValuePairHolderWithGroup.GetMember("Item2").Single(), 
                                paramInner
                                )
                            ),
                        paramOuter, 
                        paramInner
                        )
                    }
                );


            var paramGroup = Expression.Parameter(keyValuePairHolderWithGroup);
            Expression collectionSelector = Expression.Lambda(                    
                            Expression.Call(
                                    null,
                                    Enumerable_DefaultIfEmpty.MakeGenericMethod(typeof(TInner)),
                                    Expression.MakeMemberAccess(paramGroup, keyValuePairHolderWithGroup.GetProperty("Item2"))) 
                            ,
                            paramGroup
                        );

            Expression newResultSelector = new ResultSelectorRewriter<TOuter, TInner, TResult>(resultSelector).CombinedExpression;


            var selectMany1Result =
                Queryable_SelectMany.MakeGenericMethod(keyValuePairHolderWithGroup, typeof(TInner), typeof(TResult))
                .Invoke(
                    "ThisArgumentIsIgnoredForStaticMethods", new object[]{
                        groupJoin,
                        collectionSelector,
                        newResultSelector
                    }
                );
            return (IQueryable<TResult>)selectMany1Result;
        }
    }

这是我的HTML:

        var Tiers = Parse.Object.extend("Tiers");
        var queryObject = new Parse.Query(Tiers);
        var lesTiers = new Array();

        //queryObject.equalTo("isActive", true);

        queryObject.find({
            success: function (results) {
                for (var i = 0; i < results.length; i++) {
                    tiers = results[i];
                    lesTiers[i] = {
                        nom: tiers.get('nomTiers'),
                        prenom: tiers.get('prenTiers')
                    };
                }
                console.log(lesTiers);
                this.lestiers = lesTiers;
            },
            error: function (error) {
                alert("Error: " + error.code + " " + error.message);
            }
        });

=============================================== ====================

我的查询很好,它返回一个对象数组。当我尝试用数组替换孔请求时,我自己就像这样构建:

    <ion-list>
        <ion-item *ngFor="#tiers of lestiers">
            {{tiers.nom}} {{tiers.prenom}}
        </ion-item>
    </ion-list>

完美无缺。然后,我想这一行:

this.lestiers = lesTiers;

不起作用。看来我在这条线上丢失了所有数据。

也许我忘记了重要的事情?

2 个答案:

答案 0 :(得分:0)

我建议尝试以下方法:

  • 使用push方法将元素添加到数组中
  • 保存组件,以便在文字对象中使用它(在函数success中,...)。我认为此级别的this关键字不是组件中的关键字,而是来自文字对象的关键字

答案 1 :(得分:0)

非常感谢您的回答!

我试图将我的数组赋值放在我的查询之外,它运行良好! 我不确定这是更好的方法,但最终会有效。