Angularjs demonstrates the Service function and angularjsservice

Source: Internet
Author: User

Angularjs demonstrates the Service function and angularjsservice

In angularjs, We can customize our own service. It can be said that it is a custom method, a function.

Let's demonstrate it step by step:
First, define an app for angularjs:



Var demoApp = angular. module ('demoapp ', []);Source Code

 

The demoApp name is the defined app. The focus of this article is to define a service:

 

DemoApp. service ('calcservice', function () {this. addition = function (num1, num2) {return num1 + num2;} this. substruction = function (num1, num2) {return num1-num2;} this. multiplication = function (num1, num2) {return num1 * num2;} this. division = function (num1, num2) {return num1/num2 ;}});Source Code

 

Angularjs has another controller:

 

DemoApp. controller ('democontroller', function ($ scope, calcService) {$ scope. result1 = calcService. addition (12, 4); $ scope. result2 = calcService. substruction (14, 3); $ scope. result3 = calcService. multiplication (7, 9); $ scope. result4 = calcService. division (15, 3 );});Source Code

 
Finally, implement the ASP. net mvc View:

 

Result of running the program:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.