Angular / Ionic ng-repeat + ng-init $ scope变量参数传递

时间:2017-02-16 03:26:55

标签: javascript angularjs ionic-framework

我试图使用ng-repeat的结果调用ng-init中的函数,但显示的唯一图像是传递给范围变量的最后一个图像。

如果我正确地执行此操作,有任何提示或建议吗?

模板

<ion-item ng-repeat="feed in feeds" ng-if=fbfeed.message ng-init='addAttachment(feed.id)'>
    <div class="list card">
        <div class="item item-body">
            <img src="{{attachment.media.image.src}}">
        </div>
    </div>
</ion-item>

控制器

$scope.addAttachment = function (postId) {
    var attachmentData = MediaManager.getAttachments( postId );
        attachmentData.then(function(result) {
        if (result.data.length) {
            $scope.attachment = result.data[0];
        }
    });
};

1 个答案:

答案 0 :(得分:0)

您需要为所有对象添加 attachment 字段。所以将其添加到对象 feed

您可以传递Feed id并在函数中指定相应的图像。

<强> HTML

<ion-item ng-repeat="feed in feeds" ng-if=fbfeed.message ng-init='addAttachment(feed)'>
    <div class="list card">
        <div class="item item-body">
            <img src="{{feed.attachment.media.image.src}}">
        </div>
    </div>
</ion-item>

控制器:

 $scope.addAttachment = function (feed) {
    var attachmentData = MediaManager.getAttachments( feed.postId );
        attachmentData.then(function(result) {
        if (result.data.length) {
            feed.attachment = result.data[0];
        }
    });
};