Skip to content

Instantly share code, notes, and snippets.

@kalbarczyk
Created November 18, 2014 13:58
Show Gist options
  • Save kalbarczyk/6cbfd9502040c26afe21 to your computer and use it in GitHub Desktop.
Save kalbarczyk/6cbfd9502040c26afe21 to your computer and use it in GitHub Desktop.
AngularJS - funkcja bind - przykład z naszej książki „AngularJS - Pierwsze kroki”, która ukaże się na początku 2015r. dzięki wydawnictwu Helion.
<!DOCTYPE html>
<html data-ng-app="app">
<head>
<title>AngularJS - funkcje - angular.bind</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">
<h1>Otwórz konsolę aby zobaczyć efekt (F12).</h1>
</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/angular.js"></script>
<script>
var app = angular.module('app', []);
app.controller('defaultCtrl', function ($scope) {
// twozymy obiekt z właściwością text oraz funkcją logującą ten tekst
$scope.test = {
text: 'Witaj Świecie!',
printText: function () {
console.log('Napis: ' + this.text);
}
};
$scope.test.printText(); // wynik: 'Napis: Witaj Świecie!'
$scope.logText = $scope.test.printText;
$scope.logText(); // wynik: undefined
$scope.logText1 = angular.bind($scope.test, $scope.test.printText);
$scope.logText1(); // wynik: 'Napis: Witaj Świecie!'
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment