Created
November 18, 2014 14:17
-
-
Save kalbarczyk/be07226727ca40dcd90d to your computer and use it in GitHub Desktop.
AngularJS - funkcja element - 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.element</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"> | |
<h4>W polu numer 1 wpisz liczbę 1000, następnie w polu numer 2 wpisz liczbę 2000.</h4> | |
<div data-lucky-number=""></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/angular.js"></script> | |
<script> | |
var app = angular.module('app', []); | |
app.controller('defaultCtrl', function ($scope) {}); | |
app.directive("luckyNumber", function () { | |
var alertElement = angular.element("<div class=\"well\">{{data.number1}}<br />{{data.number2}}</div>"); | |
var link = function (scope) { | |
scope.$watch("data.number1", function (value) { | |
if (value === "1000") { | |
alertElement.fadeOut(800); | |
} | |
}) | |
scope.$watch("data.number2", function (value) { | |
if (value === "2000") { | |
alertElement.fadeIn(800); | |
} | |
}) | |
} | |
return { | |
restrict: "AE", | |
replace: true, | |
template: "<div>Numer 1: <input class=\"form-control\" type=\"text\" ng-model=\"data.number1\">" | |
+ "<br />Numer 2: <input class=\"form-control\" type=\"text\" ng-model=\"data.number2\"><div>", | |
compile: function (tElem) { | |
tElem.append(alertElement); | |
return link; | |
} | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment