I'm from JQuery, and my first steps with AngularJs are getting me a little bit confused...
My case :
I have a panel (#floatingpanel) that is hidden with css (top:-50px), out of the viewport.
I have a link (a.trigger), in the deepest of my app, and when you click on that link the "top" property of my panel goes from "-50px" to "0px" to make it visible (you click to toggle the visibility).
The panel is just at this end of the main container of my page, but the link it's in hundreds of others div
.
Let's have a look to the DOM structure :
<body>
<div class="main-wrapper" ng-app="myapp">
<div class="app">
<div class="many-things">
[...]
<a href="#" class="trigger" ng-click="toggleMyPanelVisibility()">
Click here to see the panel
</a>
[...]
</div>
</div><!-- .app -->
<div id="floatingpanel">
Hey ! I was hidden !
</div>
</div> <!-- #mainwrapper -->
</body>
In jQuery, It would be :
$('a.trigger').click( function(e) {
e.preventDefault();
var el = $('#floatingpanel');
var top = el.css('top');
if (top<0) el.css('top', 0);
else el.css('top', -50);
});
How can I make it with AngularJs ?
I would do it like that :
var app = angular.module('myapp');
app.run(["$rootScope", function($rootScope) {
// Toggle du myaccountpanel
$rootScope.floatingpanelVisibility = false;
$rootScope.toggleMyPanelVisibility = function() {
$rootScope.floatingpanelVisibility = !$rootScope.floatingpanelVisibility;
var panel = angular.element( $('#floatingpanel') );
var top = -50;
if ($rootScope.floatingpanelVisibility)
top = 0;
panel.css({'top': top });
}
});
Ok, it works but... i guess my app.run
will be overloaded very soon if I continue this way, no ?
I also can do it with a controller on my "main-wrapper" but it's stupid, it's already my app scope !
How can I do it with a directive ?
What could be the "best way" to solve that mess ?