Created
November 18, 2014 14:26
-
-
Save kalbarczyk/18f909ff610fec30c707 to your computer and use it in GitHub Desktop.
AngularJS - funkcja injector - przykład 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 data-ng-app="app"> | |
<head> | |
<title>AngularJS - funkcje - angular.injector</title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" /> | |
</head> | |
<body> | |
<div data-ng-controller="defaultCtrl"> | |
<input class="form-control" data-ng-model="data.name" /> | |
<button class="btn btn-success" data-ng-click="add(data.name); data.name=''">Dodaj</button> | |
<ul> | |
<li data-ng-repeat="name in list() track by $index">{{name}} <a href="#" data-ng-click="rem($index)">Usuń </a> | |
</li> | |
</ul> | |
<pre> | |
list = {{list()}} | |
</pre> | |
</div> | |
<script src="https://code.angularjs.org/1.3.0/angular.js"></script> | |
<script> | |
var app = angular.module('app', []); | |
app.controller('defaultCtrl', function ($scope, $injector) { | |
$scope.list = function () { | |
var mountain = $injector.get('mountain'); | |
return mountain.mountainsList | |
} | |
$scope.rem = function (index) { | |
var mountain = $injector.get('mountain'); | |
mountain.mountainsList.splice(index, 1); | |
}; | |
$scope.add = function (name) { | |
var mountain = $injector.get('mountain'); | |
mountain.mountainsList.push(name); | |
} | |
}); | |
app.service('mountain', function () { | |
this.mountainsList = ["Mount Everest", "K2", "Kangczendzonga", "Lhotse", "Makalu"]; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment