随着AngularJS,我怎么能有条件地添加基于动作类? [英] With AngularJS, how can I conditionally add a class based on an action?

查看:97
本文介绍了随着AngularJS,我怎么能有条件地添加基于动作类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的的jQuery code如下:

  $("body").on("click", ".nav-bar", function(e){
    $(".my-nav").addClass("open");
  });

我要角IFY这一点。有几个不同的控制器/视图,将有一个 .nav吧类,所以我pretty肯定我将不得不使用一个指令。我只是不知道如何做到这一点。理想情况下,我能剥去所有的jQuery

I want to angular-ify this. There are a few different controller / views that will have a .nav-bar class, so I'm pretty sure I'll have to use a directive. I'm just not sure how to do that. Ideally, I can strip away all jQuery.

感谢

推荐答案

创建一个指令:

.directive("navbarAction", function() {
    return {
        restrict: 'A', //<---Look up what this does, there are other options
        link: function(scope, elem, attrs) {
            //Using jQuery
            $(elem).click(function() {
                $(".my-nav").addClass("open");
            });
        }
    }
});

然后在你的 .nav吧元素中,添加一个导航栏动作属性。

Then in your .nav-bar element, add a navbar-action attribute.

示例:

<input type='button' value='Click me!' navbar-action />

角还带有jqLit​​e,(你猜对了,jQuery的一个精简版的版本),所以导入整个jQuery的lib目录前,看看你能不能做到你需要什么jqLit​​e

Angular also comes with jqLite, (you guessed it, a lite version of jQuery), so before importing the entire jQuery lib, see if you can accomplish what you need with jqLite.

这篇关于随着AngularJS,我怎么能有条件地添加基于动作类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆