Skip to content

Instantly share code, notes, and snippets.

@davelnewton
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davelnewton/7978ebc4897ea32096ee to your computer and use it in GitHub Desktop.
Save davelnewton/7978ebc4897ea32096ee to your computer and use it in GitHub Desktop.
Why AngularJS* Will Succeed
$(function() {
$('#trigger').on('click', function(e) {
e.preventDefault();
$.ajax({
url: '/books/1/simpleDom',
success: function (data) {
$('#target').html(data);
},
fail: function (data) {
alert("AJAX failed!");
}
});
});
});
<a href="#" id="trigger">Fetch Book Data</a>
<div id="target">
Replacement Target
</div>
<span class="title">
<%= @book.title %>
</span>
<span class="author">
<%= @book.author %>
</span>
<span class="isbn">
<%= @book.isbn %>
</span>
<span class="available">
<%= @book.available %>
</span>
var simpleExample = {};
simpleExample['SimpleController'] = {
trigger: function () {
$.ajax({
url: '/books/1/simpleDom',
success: function (data) {
$('#target').html(data);
},
fail: function (data) {
alert("AJAX failed!");
}
});
return false;
}
}
<a href="#" onclick="return simpleExample.SimpleController.trigger()">Fetch Book Data</a>
<div id="target">
Replacement Target
</div>
var mod = angular.module('simpleExample', []);
mod.controller('SimpleController', function($scope, $http) {
$scope.trigger = function(event) {
console.log(event.target);
$http({ method: 'get', url: '/books/1/simpleDom.json' })
.success(function (data) {
$scope.book = data;
})
.error(function (data) {
alert('AJAX failed!');
});
};
});
<div ng-app="simpleExample">
<div ng-controller="SimpleController">
<a ng-click="trigger()">Fetch Book Data</a>
<div>
<span class="title">
{{ book.title }}
</span>
<span class="author">
{{ book.author }}
</span>
<span class="isbn">
{{ book.isbn }}
</span>
<span class="available">
{{ book.available }}
</span>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment