Use Git or GitHub Desktop to push each exercise to GitHub
NOTE: Make projects all non-strict mode
- Create a new angular project called
property-binding-image-size
- In app.component.ts, create two instance properties called width and height. Both properties have a type of number and a value between 100 - 500 (you choose the number)
- In app.component.html, create an img element (see reference for this exercise) and include its src attribute. Then, copy an image address link from google (make sure it has an image extension at the end of the link) and set src to the image link.
- Now, add two more attributes: width and height, and use property binding to set it to the instance properties you made in your app.component.ts file.
Reference: https://www.w3schools.com/tags/att_height.asp
- Create a new angular project called
basic-event-binding-exercise
- In app.component.html, create a button element with the content "Click me to log to the console!"
- Implement an event binding to the button element you created so that, whenever you click the button, it logs a message(could be anything) to the console.
- run 'ng serve' and click on the button. You should be able to see something output to the console in your browser's dev tools.
Exercise 3: Dynamically disable and enable the input field using event binding and property binding.
- Create a new angular project called
dynamic-button-event-binding
- In app.component.ts, create a instance property called isDisabled of type boolean and set it to true.
- In app.component.html, create an input element and incorporate property binding by disabling or enabling the input element depending on the value of the instance property isDisabled.
- Create a button element with content "Click me to change the value of isDisabled!"
- Use event binding for whenever you click the button, you set isDisabled to the opposite of its current value. Example: If isDisabled is true when I click on the button, isDisabled is set to false.
- In the button element content, it currently has "Click me to change the value of isDisabled!", use the ternary operator and string interpolation to be able to output "Currenlty disabled" when the isDisabled property is true, and vice versa for when it is false.
- run
ng serve
. You should be able to type into the input field when isDisabled is false and vice versa for when it is true.
- Create a new angular project called
basic-ngmodel-exercise
- In app.component.ts, create a instance property called title of type string and set it equal to "header"
- In app.component.html, create an h1 element and use string interpolation to output the value of the instance property header.
- Create an input element that implements two-way data binding, ngModel, that will dynamically change the header's value for whenever you type into the input field.
- run
ng serve
to confirm this works.
- Create a new angular project called
input-event-binding-exercise
- In app.component.ts, create a instance property called title of type string and set it equal to "header"
- In app.component.html, create an h1 element and use string interpolation to output the value of the instance property header.
- Create an input element that implements event binding and property binding to get the same effect of ngModel
Notes: You will need to use attributes
input
for event binding andvalue
for property binding
Reference:
https://www.w3schools.com/tags/att_input_value.asp -> learn what value
does to an input element
You can incorporate event binding to any event listener onClick,onMouseenter, OnInput(https://www.w3schools.com/jsref/dom_obj_event.asp)
The specific event listener we want to use is input
; every time the user types, input will trigger an event. Be sure to use the $event to get its value. log to the console the value. You will see that event.target.value will get you whatever the user type.
<input (input)="" />
Exercise 6: Use ngIf and a instance property to show the element; if the instance property is false, it should not show the element
- Create a new angular project called
basic-ngif-exercise
- In app.component.ts, create a instance property called isShow of type boolean and set it to true.
- In app.component.html, create a paragraph element with the content of "Show me when isShow is true! Otherwise, don't show me!"
- Use ngIf to show the paragraph element for when isShow is true conditionally, otherwise don't show it if it's false.
- run 'ng serve', isShow is set to true, so you should see your paragraph element there.
- Set isShow to false, and it shouldn't show on the webpage.
- Create a new angular project called
basic-ngif-else-exercise
- In app.component.ts, create a instance property called isShowContent of type boolean and set it to true
- In app.component.html, create a paragraph element with content "If isShowContent is true, show me!" and another paragraph with content "Use else to show me!"
- use ngIf and else to show either or depend on the value of isShowContent.
- Create a button element with content "Click me to reverse the value of isShowContent!" Use event binding to reverse the value of isShowContent.
- Create a new angular project called
basic-ngStyle-exercise
- In app.component.ts, create a instance property called myColor of type string and set it to "green"
- In app.component.html, create a paragraph element with the content "This should change when we implement ngStyle!"
- Implement ngStyle and use myColor to change the background color of the paragraph element.
- run 'ng serve' The paragraph element should have the background color of green.
- Create a new angular project called
dynamically-change-content-ngstyle-exercise
- In the app.component.ts, create a instance property called changeColor, make it of type string and set it to 'green'
- In app.component.html, create an input element. Whenever you type into this input field, it should dynamically change the value of changeColor to whatever you type. You can use either ngModel or event binding to do this.
- Create a paragraph element. Use string interpolation to output the value of changeColor between the tags of the paragraph element.
- Use ngStyle and changeColor to change the color of the content that the paragraph element holds.
- run
ng serve
. So, for example, if you type in yellow, the color of the text should change to yellow.
- Create a new angular project called basic-ngFor-exercise
- In app.component.ts, create a instance property called foods, make it type array of strings, and set its array to pre-set strings.
- In app.component.html, use ngFor to output the array list of foods
- run 'ng serve' you should be able to see each element of the array on the web page