AngularJS Expressions: Bringing HTML to Life - FadiZahhar/AngularJs GitHub Wiki

The StartApp Team Learns to Think in Curly Braces


Scene: Back at the Whiteboard

After their first experiments, Sara, Mike, and Lina meet again to tackle a new feature request:

Boss:

“I want a dashboard where the totals, names, and calculations update automatically. Make it feel alive!”

Mike: “I read that AngularJS ‘expressions’ let us add logic and calculations right inside our HTML. Let’s figure it out!”


What is an AngularJS Expression?

Sara (reading aloud):

“An AngularJS expression is like a tiny piece of JavaScript, but you put it right in your HTML, inside double curly braces: {{ ... }}.”

Lina: “So, no more writing out document.getElementById or calling functions for small stuff?”

Mike: “Right! We can use expressions for math, strings, variables, even arrays and objects—straight in the markup.”


Trying Expressions: The Team’s Experiments

1. Math and Calculations

They open up their app:

<div ng-app>
  <p>10 + 7 = {{ 10 + 7 }}</p>
  <p>Price after discount: {{ 200 - 30 }}</p>
</div>

Result: 10 + 7 = 17 Price after discount: 170


2. Variables from Scope

Mike: “Let’s initialize some variables using ng-init.”

<div ng-app ng-init="a=5; b=15">
  <p>a = {{ a }}, b = {{ b }}</p>
  <p>a + b = {{ a + b }}</p>
  <p>a * b = {{ a * b }}</p>
</div>

Result: a = 5, b = 15 a + b = 20 a * b = 75


3. String Expressions

Lina wants a welcome message:

<div ng-app ng-init="user='Lina'">
  <p>Welcome, {{ user + '!' }}</p>
  <p>Uppercase: {{ user.toUpperCase() }}</p>
</div>

Result: Welcome, Lina! Uppercase: LINA


4. Expressions in Inputs

Sara tries mixing user input and expressions:

<div ng-app>
  <input ng-model="score1" placeholder="Score 1">
  <input ng-model="score2" placeholder="Score 2">
  <p>Total: {{ score1 * 1 + score2 * 1 }}</p>
  <p>Average: {{ (score1 * 1 + score2 * 1) / 2 }}</p>
</div>

(Again, * 1 makes sure the values are treated as numbers.)


5. Working with Objects and Arrays

Mike wants to show a summary of a task object:

<div ng-app ng-init="task={name:'Design', hours:3}">
  <p>Task: {{ task.name }}</p>
  <p>Estimated Hours: {{ task.hours }}</p>
</div>

Or a list:

<div ng-app ng-init="fruits=['Apple','Banana','Mango']">
  <p>First fruit: {{ fruits[0] }}</p>
  <p>All: {{ fruits }}</p>
</div>

Team Discussion: How is this Different from JS?

  • Sara: “No need for extra JavaScript files for simple logic.”
  • Mike: “Expressions can’t have assignments or flow control, so they’re safe.”
  • Lina: “It keeps the HTML clean, and I can read what’s going on easily!”

A Quick Challenge for the Team

Can you show a message that says “You passed!” if the average of two scores is 60 or more, and “Try again!” otherwise?

Sara’s solution:

<div ng-app>
  <input ng-model="s1" placeholder="Score 1">
  <input ng-model="s2" placeholder="Score 2">
  <p>
    {{ ((s1*1 + s2*1)/2) >= 60 ? "You passed!" : "Try again!" }}
  </p>
</div>

Key Takeaways

  • AngularJS expressions let you put logic, variables, and calculations directly in your HTML.
  • They’re simple, safe, and easy to read.
  • Perfect for dashboards, live data, or showing user feedback instantly.

Looking Ahead

Mike: “These expressions are powerful! Next, let’s look at using them with lists and dynamic data—maybe with ng-repeat?”

Lina: “Can’t wait—this feels like designing a spreadsheet in the browser!”

⚠️ **GitHub.com Fallback** ⚠️