【问题标题】:AngularJS: ng-repeat combined with JQueryAngularJS:ng-repeat 结合 JQuery
【发布时间】:2014-07-06 11:23:10
【问题描述】:

这是我的代码:

index.html

<div ng-controller="WebsitesController as portfolioWebsites" class="content">
<ul class="grid cs-style-1">
    <li ng-repeat="website in portfolioWebsites.items">
        <figure>
            <img data-src="img/{{website.img}}" data-effect="fadeInUp" alt="{{website.imgAlt}}">
            <figcaption>
                <div class="figcaption-container-inside">
                    <div class="element-title">{{website.title}}</div>
                    <div class="element-description-1">{{website.description}}</div>
                    <div class="element-description-2">{{website.date}}</div>
                    <button type="button" class="btn btn-dead coming-soon lang" ng-click="showLightDialog(website.dialog)"></button>
                </div>
            </figcaption>
        </figure>
    </li>
</ul>

script.js

app.controller('WebsitesController', function() {
this.items = websites;
}); 
var websites = [
    {
        title: "LightWolf Studios",
        description: "Our new website  Smiley smile ",
        date: 2014,
        img: "portfolio-lightwolf-studios-2014.png",
        imgAlt: "Portfolio - LightWolf Studios",
        dialog: "$('#lightDialog9')",
    },
    {
        title: "Les Zappings",
        description: "Spectacle for children",
        date: 2011,
        img: "portfolio-zappings.png",
        imgAlt: "Portfolio - Les Zappings",
        dialog: "$('#lightDialog1')",
    }
];

除了一个细节外,一切正常:在HTML 中,在每次迭代中,onclick 包含参数lightDialog9,而不是采用我在 JS 文件中写的内容(lightDialog1lightDialog2 等) .

有人会知道这个问题吗?或者是否有更好的方法来处理这种情况,例如不使用 JQuery?

谢谢 =)

编辑:这是我的函数LightDialog的代码:

function showLightDialog(selectedDialog) {
    $(selectedDialog).removeClass('dialog-hidden').addClass('dialog-shown');
    $('.hide-website').removeClass('dialog-hidden').addClass('dialog-shown');
}

基本上,它只添加能够显示或隐藏 div 的类。每个项目有一个 div。这是我创建的模态系统。

【问题讨论】:

  • 这不是这样做的方法。不要将 jQuery 与 Angular 一起使用。 showLightDialog 是做什么的?
  • 嗨 Mosho,感谢您的回答,我用函数的代码编辑了我的帖子。您可以在lightwolf-studios.com 上查看我的网站或在lightwolf-studios.com/index2.html 上查看测试

标签: javascript jquery angularjs angularjs-ng-repeat


【解决方案1】:

使用 Angular 的正确方法是这样的:

$scope.showLightDialog = function(website) {
  $scope.dialog = website.title;
}

而 html 代码会是这样的:

<div class="dialog" ng-class="{dialog-shown: dialog == 'LightWolf Studios', dialog-hidden: dialog != 'LightWolf Studios'}">LightWolf Studios</div>
<div class="dialog" ng-class="{dialog-shown: dialog == 'Les Zappings', dialog-hidden: dialog != 'Les Zappings'}">Les Zappings</div>

最好在 object.id 之上执行类似 with 的操作,但由于您的示例中没有 id,我使用了 title。

【讨论】:

  • 谢尔盖您好,感谢您的回答。对不起,我是 AngularJS(以及 Javascript)的真正初学者,所以我不太明白你的答案。我有 4 段代码:在 HTML 中,我有用于显示/消失的 div 弹出插件的代码。 2,我有我的网站列表的代码(当用户点击它们时,它会出现第一段代码)。然后我有让出现和消失的 JQuery 函数。最后,我有角度脚本来使第二段代码与不同的项目重复。你现在能更精确吗?非常感谢
  • 所以,我给你一个没有 jquery 要求的解决方案。要在 Angular 控制器中添加的功能以替换 showLightDialog。以及要添加到您的弹出 div 标记中的代码。
  • 问题是显示或隐藏不够好,我需要在点击时添加或删除css类,因为有很多与添加/删除css类相关的CSS效果。
  • 基本上,我需要知道AngularJS是否可以使用此JQuery代码: $('.div1').click(function () { $('.div2').removeClass('myClass' ) }
  • 您不需要添加/删除类。为此有 ng-class。我会更新这个案例的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 2016-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多