Created
November 18, 2014 12:54
-
-
Save kalbarczyk/b6d16bdc134d87aacb9c to your computer and use it in GitHub Desktop.
AngularJS - dyrektywa ngRepeat - przykład 2 z naszej książki „AngularJS - Pierwsze kroki”, która ukaże się na początku 2015r. dzięki wydawnictwu Helion.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="app"> | |
<head> | |
<title>ngRepeat</title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" /> | |
</head> | |
<body data-ng-controller="defaultCtrl"> | |
<div class="container"> | |
<div data-ng-repeat="mountain in mountainsList | nameLimit:'mountain':'^K'"> | |
{{mountain.mountain}} - {{mountain.metres}} | |
</div> | |
<br /><br /> | |
<div data-ng-repeat="(mountain, metres) in mountainsList2"> | |
{{mountain}} - {{metres}} | |
</div> | |
</div> | |
<script src="http://code.jquery.com/jquery-1.11.1.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> | |
<script src="https://code.angularjs.org/1.3.0-rc.1/angular.js"></script> | |
<script> | |
var app = angular.module('app', []); | |
app.controller('defaultCtrl', function ($scope) { | |
$scope.mountainsList2 = { | |
"Mount Everest": 8850, | |
"K2": 8611, | |
"Kangczendzonga": 8598, | |
"Lhotse": 8501, | |
"Makalu": 8463, | |
"Cho Oyu": 8201 | |
}; | |
$scope.mountainsList = [ | |
{ mountain: "Mount Everest", metres: 8850}, | |
{ mountain: "K2", metres: 8611 }, | |
{ mountain: "Kangczendzonga", metres: 8598 }, | |
{ mountain: "Lhotse", metres: 8501 }, | |
{ mountain: "Makalu", metres: 8463 }, | |
{ mountain: "Cho Oyu", metres: 8201 }]; | |
}); | |
app.filter('nameLimit', function () { | |
return function (input, field, expression) { | |
var out = []; | |
var pattern = new RegExp(expression); | |
for (var i = 0; i < input.length; i++) { | |
if (pattern.test(input[i][field])) | |
out.push(input[i]); | |
} | |
return out; | |
}; | |
}); | |
</script> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment