1. Trang chủ
  2. » Thể loại khác

Các Service trong AngularJS | 26 bài hướng dẫn AngularJs miễn phí PDF

3 179 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Các Service trong AngularJS

    • Sử dụng phương thức factory

    • Sử dụng phương thức service

    • Ví dụ

    • Kết quả

Nội dung

http://vietjack.com/angularjs/index.jsp Copyright © vietjack.com Các Service AngularJS AngularJS hỗ trợ khái niệm "Seperation of Concerns - Chia để trị" sử dụng cấu trúc service Service hàm JavaScript có nhiệm vụ task định Nó làm cho chúng thành thực thể riêng rẽ dễ dàng việc bảo trì kiểm thử Controller, filter gọi chúng cách đơn giản Service thường inject sử dụng chếdependency injection AngularJS AngularJS cung cấp nhiều service định nghĩa cho trước: $http,$scope,$route,$window,$location…Mỗi service có nhiệm vụ định Ví dụ, $http sử dụng để tạo ajax request lên server để lấy liệu $route sử dụng để định nghĩa thông tin routing … Những service mặc định AngularJS bắt đầu biểu tượng $ Có cách để tạo service là:  factory  service Sử dụng phương thức factory Khi sử dụng factory method, định nghĩa factory gắn method cho var mainApp = angular.module("mainApp", []); mainApp.factory('MathService', function() { var factory = {}; factory.multiply = function(a, b) { return a * b } return factory; }); Sử dụng phương thức service Sử dụng service method, định nghĩa service sau gán method với Chúng ta inject service có sẵn cho mainApp.service('CalcService', function(MathService){ http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/angularjs/index.jsp Copyright © vietjack.com this.square = function(a) { return MathService.multiply(a,a); } }); Ví dụ Dưới ví dụ minh họa cho dẫn bên testAngularJS.jsp Angular JS Services

AngularJS Sample Application

Enter a number: X2

Result: {{result}}

var mainApp = angular.module("mainApp", []); mainApp.factory('MathService', function() { var factory = {}; factory.multiply = function(a, b) { return a * b } return factory; }); mainApp.service('CalcService', function(MathService){ this.square = function(a) { http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/angularjs/index.jsp Copyright © vietjack.com return MathService.multiply(a,a); } }); mainApp.controller('CalcController', function($scope, CalcService) { $scope.square = function() { $scope.result = CalcService.square($scope.number); } }); Kết Mở trang textAngularJS.jsp trình duyệt web xem kết quả: http://vietjack.com/ Trang chia sẻ học online miễn phí Page ... online miễn phí Page http://vietjack.com /angularjs/ index.jsp Copyright © vietjack.com return MathService.multiply(a,a); } }); mainApp.controller('CalcController', function($scope, CalcService)... mainApp.factory('MathService', function() { var factory = {}; factory.multiply = function(a, b) { return a * b } return factory; }); mainApp .service( 'CalcService', function(MathService){ this.square...http://vietjack.com /angularjs/ index.jsp Copyright © vietjack.com this.square = function(a) { return MathService.multiply(a,a); } }); Ví dụ Dưới ví dụ minh họa cho dẫn bên testAngularJS.jsp

Ngày đăng: 02/12/2017, 19:01

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN