{"id":37,"date":"2018-09-20T19:18:00","date_gmt":"2018-09-20T19:18:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2018\/09\/20\/angularjs-tutorial-for-beginners-5-angularjs-expressions\/"},"modified":"2019-02-13T05:15:04","modified_gmt":"2019-02-13T04:15:04","slug":"angularjs-tutorial-for-beginners-5-angularjs-expressions","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/angularjs-tutorial-for-beginners-5-angularjs-expressions\/","title":{"rendered":"AngularJS Tutorial for Beginners 5 &#8211; AngularJS Expressions"},"content":{"rendered":"<p>This lesson would be brief and contains a simple exercise, make sure you do it.<\/p>\n<p><ins style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-slot=\"4209786523\"><\/ins><br \/>\nAngularJS Expressions are expression written inside a normal html file but are interpreted by AngularJS. AngularJS expressions are enclosed in double curly braces like this:<\/p>\n<div style=\"color: #555555; font-family: 'segoe ui';\"><\/div>\n<div style=\"text-align: center;\"><span style=\"color: #073763; font-family: 'georgia' , 'times new roman' , serif;\">{{ expression }}<\/span><\/div>\n<div style=\"color: #555555; font-family: 'segoe ui';\">\n<div style=\"text-align: center;\"><\/div>\n<\/div>\n<div style=\"color: #555555; font-family: 'segoe ui';\">They can also be written inside a directive using an assignment operator, for example:<\/div>\n<div style=\"color: #555555; font-family: 'segoe ui';\"><\/div>\n<div style=\"text-align: center;\"><span style=\"color: #073763; font-family: 'georgia' , 'times new roman' , serif;\">ng-model = {{ expression }}<\/span><\/div>\n<div style=\"color: #555555; font-family: 'segoe ui';\"><\/div>\n<div style=\"color: #555555; font-family: 'segoe ui';\">AngularJS will evalulate the expression, and output the result exactly where the expression is written in the html file.<\/div>\n<div style=\"color: #555555; font-family: 'segoe ui';\"><\/div>\n<div style=\"color: #555555; font-family: 'segoe ui';\">\n<p>AngularJS expression can contain strings, numbers, operators and other variables.<\/p>\n<p>Examples of AngularJS expressions are:<\/p>\n<\/div>\n<p><span style=\"color: #20124d; font-family: 'georgia' , 'times new roman' , serif;\">{{ 88 \/ 4 }}<\/span><br \/>\n<span style=\"color: #20124d; font-family: 'georgia' , 'times new roman' , serif;\">{{ firstname + &#8220;, &#8221; + lastname }}<\/span><br \/>\n<span style=\"color: #20124d; font-family: 'georgia' , 'times new roman' , serif;\">{{ (2 * 5)\/10 }}<\/span><\/p>\n<div style=\"color: #555555; font-family: 'segoe ui';\"><\/div>\n<div style=\"color: #555555; font-family: 'segoe ui';\"><b>Exercise<\/b><\/div>\n<div style=\"color: #555555; font-family: 'segoe ui';\">This exercise follows from Lesson 4.<\/div>\n<div style=\"color: #555555; font-family: 'segoe ui';\">Create a new file in the same folder you placed Lesson 4 and save it as Lesson5.html.<\/div>\n<p>&nbsp;<\/p>\n<div style=\"color: #555555; font-family: 'segoe ui';\">Place the following code in this file and take some time to go through the code to understand how the angularJS expressions are formed. Also take not of the use of arrays.<\/div>\n<div style=\"color: #555555; font-family: 'segoe ui';\">\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #557799;\">&lt;!doctype html&gt;<\/span><\/pre>\n<p><span style=\"color: #007700;\">&lt;html<\/span> <span style=\"color: #0000cc;\">ng-app<\/span><span style=\"color: #007700;\">&gt;<\/span><br \/>\n<span style=\"color: #007700;\">&lt;head&gt;<\/span><br \/>\n<span style=\"color: #007700;\">&lt;title&gt;<\/span>AngularJS Tutorial for Beginners<span style=\"color: #007700;\">&lt;\/title&gt;<\/span><\/p>\n<p><span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">&#8220;angular.min.js&#8221;<\/span><span style=\"color: #007700;\">&gt;&lt;\/script&gt;<\/span><br \/>\n<span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">&#8220;Scrips.js&#8221;<\/span><span style=\"color: #007700;\">&gt;&lt;\/script&gt;<\/span><\/p>\n<p><span style=\"color: #007700;\">&lt;\/head&gt;<\/span><\/p>\n<p><span style=\"color: #007700;\">&lt;body&gt;<\/span><br \/>\n<span style=\"color: #007700;\">&lt;h1&gt;<\/span>AngularJS Expression &#8211; Lesson 5<span style=\"color: #007700;\">&lt;\/h1&gt;<\/span><\/p>\n<p><span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">ng-app =<\/span> <span style=\"background-color: #fff0f0;\">&#8220;&#8221;<\/span> <span style=\"color: #0000cc;\">ng-init =<\/span> <span style=\"background-color: #fff0f0;\">&#8220;quantity = 1; price = 30; student = {firstname:&#8217;Kindson&#8217;,lastname:&#8217;Munonye&#8217;,regno:242}; code = &#8216;CS101&#8217;; scores = [80,90,75,73,60]&#8221;<\/span><span style=\"color: #007700;\">&gt;<\/span><br \/>\n<span style=\"color: #007700;\">&lt;p&gt;<\/span>Student {{student.firstname + &#8221; &#8221; + student.lastname}}!<span style=\"color: #007700;\">&lt;\/p&gt;<\/span><br \/>\n<span style=\"color: #007700;\">&lt;p&gt;<\/span>Code: {{code}}<span style=\"color: #007700;\">&lt;\/p&gt;<\/span><br \/>\n<span style=\"color: #007700;\">&lt;p&gt;<\/span>Cost on Books : {{price * quantity}} USD<span style=\"color: #007700;\">&lt;\/p&gt;<\/span><br \/>\n<span style=\"color: #007700;\">&lt;p&gt;<\/span>Reg No: {{student.regno}}<span style=\"color: #007700;\">&lt;\/p&gt;<\/span><br \/>\n<span style=\"color: #007700;\">&lt;p&gt;<\/span>Scores(English): {{scores[2]}}<span style=\"color: #007700;\">&lt;\/p&gt;<\/span><br \/>\n<span style=\"color: #007700;\">&lt;\/div&gt;<\/span><br \/>\n<span style=\"color: #007700;\">&lt;\/body&gt;<\/span><br \/>\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span><\/p>\n<\/div>\n<p><ins style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-slot=\"4209786523\"><\/ins><br \/>\n<b>Note on Arrays <\/b><br \/>\nArrays are used in the same way in AngularJS as in JavaScript. In the example, a one-dimensional array called scores has been initialized:<\/p>\n<pre style=\"line-height: 22.5px;\"><span style=\"background-color: #fff0f0;\">scores = [80,90,75,73,60]<\/span><\/pre>\n<p>To refer to a single element of the array you can use the index of the array and remember the the first element of the array has an index of 0.<br \/>\nSo scores[0] refers to 80<br \/>\nscores[1] refers to 90 and so on.<\/p>\n<p>I hope that by now you are very clear with AngularJS expressions. If you have any challenge, do leave a comment below. In Lesson 6, we would then examine angularJS controllers in more details.\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This lesson would be brief and contains a simple exercise, make sure you do it. AngularJS Expressions are expression written inside a normal html file &hellip; <\/p>\n","protected":false},"author":2,"featured_media":486,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[113],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/37"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":487,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/37\/revisions\/487"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media\/486"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}