1

I have a function in my BlogController which changes the height of a div.

$scope.setTopBackgroundHeight = function (screenProportion, targetDiv) {
  globalService.setTopBackgroundHeight(screenProportion, targetDiv);
};

I am using this controller on a few pages, but only want to call this function on one page. So I put the call in my view as follows.

<div id="primary" 
     class="content-area blog-page" 
     ng-controller="blogCtrl">
     {{$scope.setTopBackgroundHeight("half", ".background-container");}}
</div>

Now, this works. But is calling a function from within curly braces in the view ok to do style wise? I've tried to find examples of doing something like this in the angular way, but can't see anything. Should it be in some ng directive?

2 Answers 2

1

Yes, all DOM manipulation should be done inside directives. So In this case it'd be better if you had a directive attached to the div that called that service method.

HTML:

<div id="primary" 
     class="content-area blog-page" 
     ng-controller="blogCtrl"
     setBGHeight>
</div>

JS:

app.directive('setBGHeight', function(globalService) {
  return {
    link: function() {
      globalService.setTopBackgroundHeight("half", ".background-container");
    }
  }
));
Sign up to request clarification or add additional context in comments.

7 Comments

still rather wrong. probably a good place for ng-class or ng-style
I don't know what the actual service does so I'm not questioning if he should be using a service or not since he didn't ask for that.
I get it, thanks. Yeah that's what I wanted to know, the best way to apply functions. Cheers
Yeah, I have a feeling he's not using angular in any way it ought to be. But whether it gets called from a controller or directive, you probably shouldn't be setting DOM in a service...
@doog - the function I am calling checks the height of the window and then sets the div with the background to that height. What would be a better approach to achieving this without using a directive?
|
0

This is what directives are for. Make sure to prefix them like angular does with "ng-click" but don't use ng.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.