简体   繁体   English

如何使用ng-repeat引用范围内的特定变量

[英]How to refer to particular variable in scope, using ng-repeat

I have something like this in the view: 我的观点是这样的:

<div ng-repeat="thread in threads">
    <div id="conversation-{{thread.id}}">
         <img src="{{ imageloader.image }}"/>
    </div>
</div>

and in the controller (imageloader) function which set 并在控制器(图像加载器)功能中进行设置

imagesloader.image = 'path/to/image';

I need to refer only for particular imageloader.image not all of them, from all boxes. 我只需要针对特定​​的imageloader.image而不是全部引用。 Because at this moment (ie when I have 10 divs), and run controller function then image show up in all divs from repeat. 因为在此刻(即,当我有10个div时),并运行控制器功能,然后图像会重复出现在所有div中。 Any ideas? 有任何想法吗?

I hope I understood you right... 我希望我对你了解...

I think best solution would be to add the path of the images as property to the threads array, then replace this 我认为最好的解决方案是将图像的路径作为属性添加到线程数组,然后替换为

<img src="{{ imageloader.image }}"/>

with that 接着就,随即

<img src="{{ thread.imageProperty }}"/>

Now if you want to change, or refer to one special image, you can get, or set your arrays image property, what will also affect the visible image on your website... 现在,如果您要更改或引用一个特殊图像,则可以获取或设置数组图像属性,这也会影响网站上的可见图像...

That way you can get/set a specific image by changing your array... 这样,您可以通过更改数组来获取/设置特定图像...

So you have one "datasource" per image instead of one "datasource" for all images 因此,每个图像有一个“数据源”,而不是所有图像有一个“数据源”

Maybe you've answered your own question by now, but I just got back to it, See if this is what you need. 也许您现在已经回答了您自己的问题,但是我还是回到了上面,看看这是否是您的需要。

Check this out: JS FIDDLE 看看这个: JS FIDDLE

html HTML

<div ng-app ng-controller="ImageCtrl" ng-init="images = [
  {title: 'kitty 1', url: 'http://aasdfasdf.my.imadge/f1'},
  {title: 'kitty 2', url: 'http://aasdfasdfb.my.image/f1'},
  {title: 'kitty 3', url: 'http://aasdfasfffdf.my.imagde/f1'}]">
      <div ng-repeat="(imageIndex, image) in images">
          <p>
              image: <i> {{image.title}} </i>
              <a href="#" ng-click="showImage(imageIndex)">Show Image</a>
          </p>
          <img src="http://placekitten.com/g/200/200" ng-show="isShowing(imageIndex)" alt="{{image.title}}" />
      </div>
</div>

js JS

function ImageCtrl($scope) {
    $scope.activeImageIndex;

    $scope.showImage = function(index) {
        $scope.activeImageIndex = index;
    };

    $scope.isShowing = function(index){
        return  $scope.activeImageIndex === index;
    };
};

Should be pretty easy... Just hope I understood correctly. 应该很容易...希望我能正确理解。 lol. 大声笑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM