tutorial pengembangan web terbaru
 

AngularJS json Filter


Contoh

Menampilkan objek JavaScript sebagai string JSON:

<div ng-app="myApp" ng-controller="jsCtrl">

<h1>Customer:</h1>

<pre>{{customer | json}}</pre>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('jsCtrl', function($scope) {
    $scope.customer = {
        "name" : "Alfreds Futterkiste",
        "city" : "Berlin",
        "country" : "Germany"
    };
});
</script>
Cobalah sendiri "

Definisi dan Penggunaan

The json Filter mengkonversi objek JavaScript ke string JSON.

Filter ini dapat berguna saat debugging aplikasi Anda.

Objek JavaScript dapat setiap jenis objek JavaScript.


Sintaksis

{{ object | json : spacing }}

Nilai parameter

Value Description
spacing Optional. A number specifying how many spaces to user per indentation. The default value is 2

Contoh lebih

Contoh

Pastikan bahwa string JSON ditulis dengan 12 ruang per lekukan:

<div ng-app="myApp" ng-controller="jsCtrl">

<h1>Customer:</h1>

<pre>{{customer | json : 12}}</pre>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('jsCtrl', function($scope) {
    $scope.customer = {
        "name" : "Alfreds Futterkiste",
        "city" : "Berlin",
        "country" : "Germany"
    };
});
</script>
Cobalah sendiri "

Contoh

Objek JavaScript sebagai array:

<div ng-app="myApp" ng-controller="jsCtrl">

<h1>Carnames:</h1>

<pre>{{cars | json}}</pre>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('jsCtrl', function($scope) {
    $scope.cars = ["Audi", "BMW", "Ford"];
});
</script>
Cobalah sendiri "

Pages terkait

AngularJS Tutorial: Filter sudut