AngularJS: API trong AngularJS
API là các ký tự viết tắt của Application Programming Interface (dịch nôm na là: Giao diện Lập trình Ứng dụng).
Global API trong AngularJS
Global API trong AngularJS là một tập các hàm JavaScript global để giải quyết các công việc phổ biến như:
- So sánh đối tượng
- Xem (iterating) các đối tượng
- Chuyển đổi dữ liệu
Các hàm Global API được truy cập bằng cách sử dụng các đối tượng angular.
Dưới đây là danh sách một số hàm API phổ biến:
API | Mô tả |
---|---|
angular.lowercase() | Chuyển chuỗi thành chữ thường |
angular.uppercase() | Chuyển chuỗi thành chữ hoa |
angular.isString() | Trả về true nếu tham chiếu là chuỗi |
angular.isNumber() | Trả về true nếu tham chiếu là số |
angular.lowercase()
Ví dụ
<div ng-app="myApp" ng-controller="myCtrl">
<p>{{ x1 }}</p>
<p>{{ x2 }}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.x1 = "JOHN";
$scope.x2 = angular.lowercase($scope.x1);
});
</script>
angular.uppercase()
Ví dụ
<div ng-app="myApp" ng-controller="myCtrl">
<p>{{ x1 }}</p>
<p>{{ x2 }}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.x1 = "John";
$scope.x2 = angular.uppercase($scope.x1);
});
</script>
angular.isString()
Ví dụ
<div ng-app="myApp" ng-controller="myCtrl">
<p>{{ x1 }}</p>
<p>{{ x2 }}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.x1 = "JOHN";
$scope.x2 = angular.isString($scope.x1);
});
</script>
angular.isNumber()
Ví dụ
<div ng-app="myApp" ng-controller="myCtrl">
<p>{{ x1 }}</p>
<p>{{ x2 }}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.x1 = "JOHN";
$scope.x2 = angular.isNumber($scope.x1);
});
</script>