简体   繁体   English

使用 angular 在运行时应用边距时遇到问题

[英]Getting an issue in applying margin on the runtime using angular

I have an li element in which we are calling a method SiteAssetStyleForShiftedAsset like this:我有一个 li 元素,我们在其中调用SiteAssetStyleForShiftedAsset方法,如下所示:

<li class="holder-white title-holder" data-ng-style="{{SiteAssetStyleForShiftedAsset()}}">
 ...
</li>

and from our javascript controller it being called like this:从我们的 javascript controller 中,它被称为:

function SiteAssetStyleForShiftedAsset() {

    var isPPMJob = localStorage.getItem("IsPPMJob").toUpperCase();

    var shiftingAsset = $scope.addClassForShiftingAsset;

    if (isPPMJob == "FALSE") {

        // it is working fine here. Margin is being applied correctly. 
        return { "margin-right": "50px" };
    }
    else if (isPPMJob == "TRUE") {
        if (shiftingAsset.toUpperCase() == "TRUE")
        {
            //it is not working fine on this line. Margin is not being applied.
            return { "margin-right": "50px" };
        }
        else {
            return { "padding-right:": "15px" };
        }
    }
}

So it is working fine in the first if (isPPMJob == "FALSE") but in else if where we are checking shiftingAsset.toUpperCase() == "TRUE" that margin is not being applied.所以它在第一个if (isPPMJob == "FALSE")中工作正常,但在 else if 我们正在检查 shiftAsset.toUpperCase () == "TRUE"时,边距没有被应用。

Tried alerts on all conditions they are showing fine but margins are causing problems.尝试在所有条件下发出警报,它们显示良好,但边距会导致问题。

I found solution for that problem.我找到了解决该问题的方法。 The problem was with HTML code we were using data-ng-style like this问题出在 HTML 代码上,我们使用的是这样的 data-ng 样式

data-ng-style="{{SiteAssetStyleForShiftedAsset()}}"

instead of this we have to use it like而不是这个,我们必须像使用它一样

data-ng-style="{'margin-right': SiteAssetStyleForShiftedAsset()}"

Then in JS controller just return value of margin ie "10px", "50px",etc然后在 JS controller 中只返回边距值,即“10px”、“50px”等

function SiteAssetStyleForShiftedAsset() { function SiteAssetStyleForShiftedAsset() {

    var isPPMJob = localStorage.getItem("IsPPMJob").toUpperCase();

    var shiftingAsset = $scope.addClassForShiftingAsset;

    if (shiftingAsset == "false"){
                //alert("abc");
         return "10px";
                //return { "padding-right:": "15px" };
    }
    else{
        return "50px";
    }
}

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

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