[AngularJS] - serrvice 3
RUN
<!doctype html> <html lang="vi"> <head> <meta charset="UTF-8"> <title>Ví dụ sử dụng Service trong AngularJS - Freetuts.net</title> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> <style> *{margin:0;padding:0} body{margin:20px;} </style> <script> /* * Author: thehalfheart@gmail.com * Website: freetuts.net */ // Lưu module vào biến var module = angular.module('myServiceModule', []); // Khai báo controller module.controller('MyController', ['$scope','validate', function ($scope, validate) { $scope.xuat_thong_bao = function() { validate($scope.number); }; }]); // Tạo service kiểm tra số chẵn module.service('validate', function($window, kiem_tra_so_chan) { return function(number) { if (kiem_tra_so_chan(number)) { $window.alert("Số " + number + " là số chẵn"); } }; }); // Tạo service kiểm tra số chẵn module.service('kiem_tra_so_chan', function() { return function(number) { return(number % 2 == 0); }; }); </script> </head> <body ng-app="myServiceModule" ng-author="thehalfheart@gmail.com" ng-website="freetuts.net"> <div id="simple" ng-controller="MyController"> <p>Nhập vào số chẵn thì sẽ thông báo lên màn hình</p> <input ng-model="number" ng-change="xuat_thong_bao()"/> </div> </body> </html>
PHÓNG TO