So, this project is called “Message Board” but we haven’t even done anything with any messages yet! All we’ve done so far is the configuration and setup. Now let’s actually work on the messages portion of this website! We need to build a create component that will allow users to make a new message. Run ng generate component create in the console.
Update the routes in app.module.ts for this new view. The routes should look like the following:
Then we need to update the create link in our header.component.html to send users to this new component. You should notice a pattern now, these are the same things we'll do for most of the components we create.
And the following CSS rules into the create.component.css file.
/* Ensure the content is never covered by the footer, and is offset from the header */
.container {
margin-bottom: 7.5rem;
margin-top: 2rem;
}
/* Style the Submit and Cancel buttons */
.card-footer {
margin-top: 2rem;
background-color: #6c757d;
}
.btn {
border: 0;
font-weight: bold;
letter-spacing: 0.75px;
color: white;
background-color: #343a40;
}
.btn:first-of-type {
margin-right: 1rem;
}
.btn:hover {
color: #343a40;
background-color: #007bff;
}
You might be expecting the next steps by now, this is the same thing we did on the Register and on the Login pages! We need to create variables for the message “title” and the message “content” - and we’ll probably want a function named createMessage() too! Open create.component.ts and create those variables, and that function. Try it first on your own, then check below.
And now let’s link our input fields with our variables, make the “enter” button call the “createMessage()” function, and make the “submit” call that same function. Let's go back to our create.component.html.
Notice the lack of the (keyup.enter) on the textarea? Since we’re using a textarea for the users to input the message content, users might make messages with multiple lines. We don’t want to create the message when they try to make a new line!
Serve up your web app, and you should be able to create a message! Or at least, the contents of the form get printed into the console. We’ll use a service to push the contents of the form off to Firebase in a moment.
First we need to protect the “create” view - we don’t want unauthenticated users to create messages, we only want people who have registered to have access to that view! This introduces a feature of Angular that is called “router guards” - it’s a chunk of code that guards a particular route from unauthorized access.
Next, we'll create a router guard, and a message service!