Skip to content

Instantly share code, notes, and snippets.

@loonison123
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save loonison123/3a99dcf4e122e7de253b to your computer and use it in GitHub Desktop.

Select an option

Save loonison123/3a99dcf4e122e7de253b to your computer and use it in GitHub Desktop.
Angular.js snippets
// $watch inside a service
// Source: http://stackoverflow.com/questions/17806600/angularjs-watch-inside-a-service
$rootScope.$watch(function() {
return //value to be watched;
}, function watchCallback(newValue, oldValue) {
//react on value change here
}, true); // true if you want it to watch object properties
// Refreshing your current state. Such as you are in the ItemView state, and you are $state.go('ItemView')
$state.go($state.current, {}, {reload: true});
// Dependency injection in directive
app.directive('imageGallery', ['$timeout', function ($timeout) {
return {
// Do NOT list it in the link function, it will be undefined if you do
link: function ($scope, element, attributes) {
// Use $timeout here
}
};
}]);
// Directive that allows click and double-click event since angular doesn't support it by default
// http://jsfiddle.net/ZeNVa/
app.directive('customClickEvents', ['$timeout', function ($timeout) {
return {
link: function ($scope, element, attributes) {
$scope.clicked = true;
$scope.stopped = false;
$(element).click(function (e) {
$scope.clicked = $timeout(function () {
if ($scope.stopped == false) {
$scope.$apply(function () {
$scope.$eval(attributes.customClick);
})
}
}, 200);
}); // End click
$(element).click(function (e) {
$scope.stopped = $timeout.cancel($scope.clicked);
$scope.$apply(function () {
$scope.$eval(attributes.customDblClick);
})
}); // End click
}
};
}]);
// <ul><li ng-repeat="image in images" custom-click="viewImage(image)" custom-dbl-click="viewImageDetail(image)"></li></ul>
// Simple http example
var deferred = $q.defer();
$http.post('/your/url', {userId: 100})
.success(function (response) {
// Do what you want with response
deferred.resolve(response);
});
return defered.promise;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment