Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In you case, I think you definitely should create a directive to encapsulate the button with its behavior. Here's an example:</p> <p><strong>HTML</strong></p> <pre><code>&lt;div ng-app='app'&gt; &lt;form id="id_request" ng-controller="RequestCtrl"&gt; &lt;input id="id_title" name="title" ng-model="request.title" type="text" class="ng-valid ng-dirty" /&gt; &lt;my-button state="state" click-fn="submitRequest()"&gt;&lt;/my-button&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p><strong>Javascript</strong></p> <pre><code>angular.module('app', []) .directive('myButton', function() { return { restrict: 'E', scope: { state: '=', clickFn: '&amp;' }, template: '&lt;button type="submit" class="btn btn-primary" ng-click="clickFn()" ng-disabled="disabled"&gt;' + ' &lt;i ng-class="cssClass"&gt;&lt;/i&gt;' + ' {{ text }}' + '&lt;/button&gt;', controller: function($scope) { $scope.$watch('state', function(newValue) { $scope.disabled = newValue !== 1; switch (newValue) { case 1: $scope.text = 'Submit'; $scope.cssClass = ''; break; case 2: $scope.text = 'Submitting'; $scope.cssClass = 'icon-refresh icon-spin'; break; case 3: $scope.text = 'Error'; $scope.cssClass = 'icon-warning-sign'; break; case 4: $scope.text = 'Success'; $scope.cssClass = 'icon-ok'; break; } }); } }; }) .controller('RequestCtrl', function ($scope, $http, $timeout) { $scope.state = 1; $scope.request = {}; $scope.submitRequest = function() { $scope.state = 2; $http({ method : 'GET', url : '/ravishi/urSDM/3/', data : $.param($scope.request), headers : { 'Content-Type' : 'application/x-www-form-urlencoded', } }). success(function(data, status, headers, config) { $scope.state = 4; $timeout(function() { $scope.state = 1; }, 1500); console.log("success"); }). error(function(data, status, headers, config) { $scope.state = 3; $timeout(function() { $scope.state = 1 }, 1500); console.log("error"); }); } }); </code></pre> <p>jsFiddle <a href="http://jsfiddle.net/urSDM/12/" rel="nofollow">here</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload