Skip to content

Instantly share code, notes, and snippets.

@fdietz
Created February 19, 2013 19:19
Show Gist options
  • Save fdietz/4988951 to your computer and use it in GitHub Desktop.
Save fdietz/4988951 to your computer and use it in GitHub Desktop.
Recipes with Angular.js: Controllers - Responding to Scope Changes
function MyCtrl($scope) {
$scope.name = "";
$scope.$watch("name", function(newValue, oldValue) {
if (newValue.length > 0) {
$scope.greeting = "Greetings " + newValue;
}
});
}
<html>
<head>
<script src="js/angular.js"></script>
<script src="js/app.js"></script>
<link rel="stylesheet" href="css/bootstrap.css">
</head>
<body ng-app>
<div ng-controller="MyCtrl">
<input type="text" ng-model="name" placeholder="Enter your name">
<p>{{greeting}}</p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment