根据ng-click索引AngularJS更改图像src

 px9ctrl 发布于 2023-02-13 15:59

我有这个angularJS代码,指令模板定义:

  • 另外,我的指令代码有:

    link: function (scope, elem, attrs) {            
            scope.ImgTest= "Img_1";
    

    在ng-click上,我希望

  • 在从Img_1到Img_2之间单击之前更改所有元素上的图像.(因此,更改
  • 索引在0和$index单击之间的所有元素).

    怎么能实现这一目标?
    .. 谢谢

  • 1 个回答
    • 我们可以使用ng-switch由我正在调用的变量控制的switchPoint, switchPoint设置为$indexby toggle()).

      之前,一切都switchPoint将使用ImgTest,而一切将使用后ImgTest2.

      这里的ng-switch代码(测试电流$indexswitchPoint).

      <div ng-switch="switchPoint < $index">
          <div ng-switch-when=true>
              <img src="img/{{ImgTest}}">
          </div>
          <div ng-switch-when=false>
               <img src="img/{{ImgTest2}}">
          </div>
      </div>
      

      这是一个带有切换功能和switchPoint变量的更新链接功能.

      link: function (scope, elem, attrs) {            
           scope.ImgTest= "Img_1";
           scope.ImgTest2= "Img_2";
           scope.switchPoint = -1;
           scope.toggle= function(val) {
              scope.switchPoint= val;
          };
      }
      

      这是一个小提琴(打印{{imgTest}} ...而不是纯粹为了简单起见使用图像):http: //jsfiddle.net/ueX3r/

      2023-02-13 16:02 回答
    撰写答案
    今天,你开发时遇到什么问题呢?
    立即提问
    热门标签
    PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有