Sunday, 15 April 2012

javascript - AngularJs update directive after a call to service method -


I am trying to create a reusable warning service, which I will only say elsewhere in my application:

Alertservice Soylert ('Warning', 'Something went wrong!'); For example, after calling AJAX calls on backend API, I am using a factory and directive, but it seems that I am doing something wrong because show the instructions. Does not get updated after calling Right now I have something like this:
  var srv = angular.module ('srv', []); SRV Factory ('alert service', ['$ timeout', function ($ timeout) {var alertService = this; alertService.alertNeeded = false; alertService.alertClass = ''; alertService.alertMessage = ''; alertService.setAlertNeedNeedNeed = function ) {AlertService.alertNeeded = true}; alertService.setAlertClass = function (type) {if (type === 'warning') alertService.alertClass = 'alert-warning'; if (type === 'success') alertService AlertClass = 'alert-success'; if (type === 'info') warning service. Alert class = 'alert-information'; if (type === 'danger') warning service.LartClass = 'warning-danger' ;}; Alert service .setAlertMessage = function (message) {alertService.alertMessage = message;}; return {sh OwAlert: function (class, msg) {alertService.setAlertNeeded (); alertService setAlertClass (class); alertService.setAlertMessage (MSG);}};}]). Directive ('MyLater', ['Alert Service', Function (Alert Service)] {Reform {Ban: 'A', Template: '& lt; div ng-class = "alertclass" ng-show = "alertNeeded" & gt; {{Alert Message}} , link: function (scope) {scope.alertNeeded = alertService.alertNeeded; scope.alertMessage = alertService.alertMessage; scope.alertClass = alertService.alertClass;}}} ])). Controller ('alert showing controller', ['$ scope', 'alert service', function ($ radius, alert service) {alertService.showAlert ('warning', 'warning warning !!!')}]);  

My code does not appear at all, but I just want to show what I'm trying to do: I call alertService.showAlert (...) Want to In another module, from another controller (which depends on the srv module) and thus showing the correct alert in the myAlerts update variables .

To see that call to call Alert method values ​​are set, but in the directive code, I alertService.alertNeeded like undefined Getting.

I am totally new in Angular Jays, so maybe I am doing something wrong, but I spent the whole evening working for it and I still do not know what is the proper solution for this is.

Please have two different meanings for your code for alertService .

Within the definition of the factory, it refers to the factory itself. Everywhere, it refers to the returned item by the factory. The easiest way to move forward is to add some missing methods to the returned item by the factory:

  return {showAlert: function (cssclass, msg) {alertService.setAlertNeeded ()); AlertService.setAlertClass (cssClass); AlertService.setAlertMessage (MSG); }, AlertClass: function () {return warning: service. AlertClass; }, Alert message: function () {return warning: service. Alert message; }, Alert Required: Function () {Return Warning Service. Alert Required; }}; Then, change your directive's template so that they can call these functions on each digest cycle:  
  directive ('myAlerts', [ 'AlertService', function (warning service) {return: {a :, 'a', template: '

Then you should see your warning message. .


No comments:

Post a Comment