Tuesday 15 February 2011

angularjs - How do I maintain scope when delegating to service? -



angularjs - How do I maintain scope when delegating to service? -

i've built little service handle errormessages in application. publicly available on rootscope , able add together new messages page needed. need have clickable links in messages have arisen.

questions:

how dynamically add together javascript handled angular messages created? i've added onclicks work, ng-click seem not handled. the js run in controller created message in first place. how create sure end in right scope when clicking link in error message? if function adding message service, how solve that?

and service i'm playing around with: var myapp = angular.module('myapp', []);

function errorhandlingfactory() { this.messages = []; this.addmessage = function (messagetext, type) { var message = this.messages.push({messagetext: messagetext, type: type, closeable: false}); }; this.gethtmlcontent = function(messageid) { homecoming this.messages[messageid].messagetext; } this.removemessage = function (messageid) { this.messages.splice(messageid, 1); }; this.clearmessages = function() { this.messages = []; }; } myapp.service('errorhandling', function () { homecoming new errorhandlingfactory(); }); myapp.run(function($rootscope, errorhandling) { // attach global error handling object our rootscope $rootscope.errorfactory = errorhandling; }); // usage controller $rootscope.errorfactory.addmessage('the message added', 'warning');

to create bit easier understand, i've created jsfiddle at. http://jsfiddle.net/kxsml25h/7/

what when link in message clicked, function desiredcallback run on generictestcontroller.

angularjs angularjs-scope angular-services

No comments:

Post a Comment