Skip to content

Instantly share code, notes, and snippets.

@eriktrom
Created September 23, 2013 01:40
Show Gist options
  • Save eriktrom/6665588 to your computer and use it in GitHub Desktop.
Save eriktrom/6665588 to your computer and use it in GitHub Desktop.
<!doctype html>
<html ng-app="myApp">
<head>
<title>Simple App</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular.js"></script>
</head>
<body>
<div ng-controller="SomeController">
<input type="text"
placeholder="What city do you live in"
ng-model="city">
<my-city ng-model="city"></my-city>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('SomeController', function($scope) {
$scope.city = undefined;
// $scope.city = "Seattle"; // try me
});
app.directive('myCity', function() {
return {
restrict: 'E',
require: '^ngModel',
scope: {
ngModel: '='
},
template: 'I live in {{ city }}',
controller: ['$scope', function($scope) {
$scope.city = "Seattle";
}],
link: function(scope, el, attrs, ctrl) {
scope.$watch('ngModel', function(newCity) {
if (newCity) {
scope.city = newCity;
}
});
}
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment