Last active
August 29, 2015 14:15
-
-
Save Usse/c7fd43861d74c15ab86d to your computer and use it in GitHub Desktop.
Basic Angular Data binding
This file contains hidden or 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 ng-app="app"> | |
<head> | |
<script data-require="[email protected]" data-semver="1.4.0-beta.4" src="https://code.angularjs.org/1.4.0-beta.4/angular.js"></script> | |
<link href="style.css" rel="stylesheet" /> | |
<script> | |
(function() { | |
var app = angular.module('app', []); | |
var MainController = function($scope, $http) { | |
var onUserComplete = function(response) { | |
$scope.user = response.data; | |
}; | |
var onError = function(response) { | |
$scope.error = "Could not fetch the user."; | |
} | |
$http.get('https://api.github.com/users/usse') | |
.then(onUserComplete, onError); | |
}; | |
app.controller('MainController',MainController); | |
//app.controller('MainController',['$scope', '$http', MainController]); <-- minifier friendly version | |
}()); | |
</script> | |
</head> | |
<body ng-controller="MainController"> | |
<div>{{error}}</div> | |
<h1>{{user.name}}</h1> | |
<img ng-src="{{user.avatar_url}}" title="{{user.name}}" width="200"> | |
</body> | |
</html> |
This file contains hidden or 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 ng-app="app"> | |
<head> | |
<script data-require="[email protected]" data-semver="1.4.0-beta.4" src="https://code.angularjs.org/1.4.0-beta.4/angular.js"></script> | |
<link href="style.css" rel="stylesheet" /> | |
<script> | |
angular.module('app', []). | |
controller('MainController', ['$scope', '$http', function($scope, $http) { | |
var onUserComplete = function(response) { | |
$scope.user = response.data; | |
}; | |
var onError = function(response) { | |
$scope.error = "Could not fetch the user."; | |
} | |
$http.get('https://api.github.com/users/usse') | |
.then(onUserComplete, onError); | |
}]); | |
</script> | |
</head> | |
<body ng-controller="MainController"> | |
<div>{{error}}</div> | |
<h1>{{user.name}}</h1> | |
<img ng-src="{{user.avatar_url}}" title="{{user.name}}" width="200"> | |
</body> | |
</html> |
This file contains hidden or 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 ng-app="app"> | |
<head> | |
<script data-require="[email protected]" data-semver="1.4.0-beta.4" src="https://code.angularjs.org/1.4.0-beta.4/angular.js"></script> | |
<link href="style.css" rel="stylesheet" /> | |
<script> | |
angular.module('app', []). | |
controller('MainController', ['$scope', function($scope) { | |
var person = { | |
firstname : 'Andrea', | |
lastname : 'Usseglio', | |
imgSrc : 'https://pbs.twimg.com/profile_images/1599369214/312a6f2_200x200.jpg' | |
} | |
$scope.person = person; | |
}]); | |
</script> | |
</head> | |
<body ng-controller="MainController"> | |
<h1>{{person.firstname + " " + person.lastname}}</h1> | |
<img ng-src="{{person.imgSrc}}" title="{{person.firstname}} {{person.lastname}}"> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment