AngularJs #12 How To Create a Custom Service in Angular JS ( Tutorial )

in #utopian-io8 years ago (edited)


Image Source

What Will I Learn?

I will learn how to create a custom service in angular js .

  • How to design the table using HTML and angular directives .
  • How to create a function that process string in the controller .
  • How to create the service and link it with the controller function .

Requirements

  • Basic Knowledge of HTML and CSS language .
  • Basic Knowledge of JavaScript language .
  • Has an editor text support HTML and JavaScript .
  • Must has the angular files from the official site .

Difficulty

  • Basic

Tutorial Contents

In this tutorial we will discuss how to create and use a costume service in Angular JS , let's understand it with an example , I want to generate two text boxes the first is a string without any spaces like it ' AngularUtopianTutorial ' and I have a button named " process String " when I click on it the second text box " result " will contain our String but with spaces " Angular Utopian Tutorial " so I will include a space between the letters before the letter U and the letter T , so the first thing is to design our idea
capture-20180221-003427.png
Here I created a table with 3 table rows the first is Your String which is our string without spaces that I want to add a spaces when we click on the button , the second row has also two table data < td> the first is the input with the type text also the label result , the third one is the button which is an input with type = button and a value is " Process String " , I have linked some CSS proprieties to be more beauty
capture-20180221-003049.png
I used the border-collapse : collapse , border black with 1px finally a padding to be like it
capture-20180221-003503.png
If we click on the button nothing going to happen we need to implement this logic , I need firstly to add a ng-model for the input let's do it
capture-20180221-005123.png
So the first input it has ng-model attribute that will have as propriety " input " , similarly for the result text box will actually be displaying the result I called the model "output", and when we click on the button we want to call a function on the scope object , I used the ng-click attribute to call " transformString(input) " function with the input item , now we must define this function in our controller function
capture-20180221-011514.png
So I checked into the function if the not an input element by the sign " ! " is the opposite of our value , if it's not an input element I will return the input element we will do no process string
capture-20180221-011550.png
The first thing that am going to do is to create a variable named " output " which is an empty string and another variable named " i " which is going to be a counter to use in the for loop and I initialized it with the value " 0 " , I used a for loop the first choice is the ' i ' variable , the second is the condition the variable will be less then the length of our input text and the i will increment by 1 , inside the loop I have a condition if(i>0) why I added this condition ? just to skip the first letter because the first letter will be for example in uppercase form but I don't want to apply this transformation for it for that I skip the first letter , I added also another condition which is if the input of i of this place now equals at the letter of the input in the place i also to uppercase which is a JavaScript function if it's true the output will add a space but if the condition is false it will continue appending the letters of input to the output variable .
capture-20180221-011617.png
I attached the output to the $scope object after added the input[i] letter to the output .let's reload the page and see what's the result of our function
capture-20180221-014529.png
So I type the string that I want to add spaces which is " AngularUtopianTutorial " and when I click on the button this is the result
capture-20180221-014553.png
Great look at that it doing all what we want , so all the logic with our controller function there are two problems , the first is the controller code has become a big complex and big large , the second is when we want to reuse this function in other controller we must duplicate this code so this is simple example to use the service , so the first thing to do is to add another JavaScript file to the project
capture-20180221-020857.png
I added a reference from the custom.js file and I have create a new factory to create the new service named ' stringService ' this service has a function that's return also another function which is our function that I have created in the custom js file it will return the output variable to this function .
capture-20180221-020952.png
I included the stringService like a parameter to the controller function and I used it to call the processString function , and I included the script page in the HTML view and this is the result
capture-20180221-014553.png
The same result here by using the service method to help us because it's reusable you can use it in any controller .

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @aymenz I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.090
BTC 62154.06
ETH 1736.43
USDT 1.00
SBD 0.38