如何在Angular中设置一个属性的值

How to set the value of an attribute in Angular?

本文关键字:一个 属性 Angular 设置      更新时间:2023-09-26

在我的指令中,我有这个代码:

// hide all drodowns with this attribute
$(document).find('[dropdown]').each(function (index) {
    if ($(this).is(':visible')) {
        var a =  $(this).attr('ng-show');
        ???
    }
});

在问号处,我想做以下操作:获取ng-show属性的值并将此值设置为false。我从jQuery得到的值是,例如:showActionsDropdown。这个值是我作用域中的一个变量。

我想知道的是如何将showActionsDropdown的值更改为true

我发现我一直在寻找。我是这样完成的,使用$parse:

$(document).find('[dropdown]').each(function (index) {
    if ($(this).is(':visible')) {
        var attrValue = $(this).attr('ng-show');
        var model = $parse(attrValue); // note: $parse is injected in the ctor
        model.assign(scope, false);
    }
});

像这样简单的尝试

if ($(this).is(':visible')) {
     var a =  $(this).attr('ng-show');        
     $(this).attr('ng-show','false');        
}

像这样直接赋值给ng-show,或者你也可以像

这样
if ($(this).is(':visible')) {
     var a =  $(this).attr('ng-show');        
     $(this).hide();        
}