Last active
August 29, 2015 14:04
-
-
Save loonison123/3a99dcf4e122e7de253b to your computer and use it in GitHub Desktop.
Angular.js snippets
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
| // $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