Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should AngularJS logic be placed in HTML file?

I want to refactor code of which I post examples below. I am very new to AngularJS. Now when I saw the code, I was very curious about all the logic that is placed in the HTML code.

<p ng-show="countForm.count.$dirty && countForm.count.$error.min" class="error-message">

<button ng-click="step(2)" ng-show="data.step == 1 && countForm.count.$dirty" ng-disabled="countForm.$invalid" class="line-break">

<div ng-class="{selected: data.spreadChoice == 3}" ng-click="data.spreadChoice = 3; step(3)" ng-mouseover="data.preSpreadChoice = 3" ng-mouseout="data.preSpreadChoice = data.spreadChoice">

<div ng-show="data.step >= 2" class="step" ng-class="{active: data.step == 3, done: data.step > 3, left: data.preSpreadChoice == 1, right: data.preSpreadChoice == 3}" ng-scroll-here="data.step == 3">

<p ng-switch-when="false" class="large">[[data.emails.length]] von [[data.count]] – <span class="red">[[Math.max(0,data.count-data.emails.length)]]</span> Members</p>

<div ng-show="data.step >= 5 && data.multipleTeams"  class="step" ng-class="{done: data.step > 5, active: data.step == 5}" ng-scroll-here="data.step == 5">

<button class="small" ng-disabled="!unitsForm.$dirty || unitsForm.$invalid" ng-click="addUnit(data.nextTeam, data.nextTeamleaderEmail)">

Shouldn't the HTML rather contain classes or attributes and the logic itself should be placed in JS files or JS code? Is this a good (or at least a common) way of developing AngularJS? Or should placing logic in HTML be avoided?

like image 267
Gottlieb Notschnabel Avatar asked Jul 19 '14 09:07

Gottlieb Notschnabel


People also ask

Can you have logic in HTML?

Logical tags are used to tell the browser what kind of text is written inside the tags. They are different from physical tags because physical tags are used to decide the appearance of the text and do not provide any information about the text.

Is angular written in HTML?

Angular is a platform and framework for building single-page client applications using HTML and TypeScript. Angular is written in TypeScript.

How does AngularJS work with HTML?

How AngularJS works with HTML. The AngularJS framework works by first reading the HTML page (which has additional attributes). Angular then interprets those attributes as directives to bind input or output parts of the page to a model represented by standard JS variables.

Does AngularJS support HTML5?

The In-place Editor supports adding HTML5 input components using the template property. The Template property can be given as either a string or a query selector .


2 Answers

If you ask me:

  1. Client side business logic sits in services that are injected into directives\controllers.
  2. UI logic is suppose to be placed in the controllers.

Now about adding logic to the views, if we are talking about business logic then it's a big no no. Use a method on your controller that will evaluate stuff using the service.

If we are talking about ng-if/ng-show conditions then only if they are small and "readable" conditions I would add them to the view. When it's more than that, I move them to the controller for debugging issues and since I believe the HTML should be readable.

like image 97
Amir Popovich Avatar answered Oct 23 '22 21:10

Amir Popovich


Placing logic in HTML using directives in angular is a good way. You cannot take full advantage of angular without placing logic in HTML.

Controllers should contain view logic, but shouldn’t actually reference the DOM (referencing the DOM should only be done through directives). ref

Two things to remember or the best practices for AngularJS are

  • Treat the scope as read-only in views
  • Treat the scope as write-only in controllers
    ref

Since you are placing logic in HTML, if you treat it as read-only, you can check conditions or extract data using functions in scope, but the original data model isn't disturbed whatever you do in HTML.

Also tying dom elements to specific directives are the most powerful features of angular.

When you use a datepicker, in jQuery, you could do as follows:

<div id="datepicker"></div>

then in JS:

jQuery('#datepicker').datepicker({
    start:'today',
    end:'tomorrow',
    showTime:true
})

You can do this in angular way as follows

This way even when a designer or someone who reads HTML will be able to read what and even you can pass the options from the element's attributes itself.

<div date-picker start="today" end="tomorrow" show-time="true"></div>

AngularJS's importance itself is declarative syntax and can contain expressions as attribute values like you posted. That is not at all a bad practice. Indeed it is common and good practice all developers do. Using logic in HTML in angularjs saves a lot of code writing by ourselves. All the heavylifting is done by angular behind the scenes.

See some best practices about AngularJS

like image 5
Raghavendra Avatar answered Oct 23 '22 21:10

Raghavendra