简体   繁体   English

ng-显示当前模板中是否存在html元素?

[英]ng-show if html element exists in current template angular?

I have a help button which I want to show in the layout only if there is a specific div #helpId in the template. 我有一个帮助按钮,仅当模板中有特定的div #helpId时,才希望在布局中显示它。 (when clicked will display #helpId in a modal window). (单击后将在模式窗口中显示#helpId)。

Is this possible without setting a variable in every controller? 如果不在每个控制器中都设置变量,是否可以做到这一点?

Thank you! 谢谢!

Maybe I miss the point but if you use ng-show you have to provide some function to detect the wanted div #helpid anyway. 也许我错过了重点,但是如果您使用ng-show,则无论如何必须提供一些功能来检测想要的div #helpid。 This function could contain some jQuery code to find the element. 该函数可能包含一些jQuery代码来查找元素。

But I would suggest to implement a little service holding the state of your app. 但我建议您实施一个可以保留应用程序状态的小服务。 Something like that (you can extend this approach): 诸如此类(您可以扩展此方法):

myapp.service('Context', function () {
    var context = {}; // current status
    return {
        get: function () { 
            return context;
        },
        set: function (state) { 
            context.state = state;
            return context;
        }
    };
};

You can use a $rootScope variable instead (as you already suggested) but polluting the $rootScope isn't such a good idea. 您可以改用$ rootScope变量(如您已经建议的那样),但是污染$ rootScope并不是一个好主意。

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

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