Actually, in your example, it looks like you want to override it just for
that instance? (more monkey patch). If you change the prototype, it will
apply to new instances not yet created.

Do you mean:

function MyCtrl($scope, MyObject) {
    var obj1 = new MyObject();

      //override some method:
      obj1.getText = function() {
        return 'override';
      }

    $scope.text = obj1.getText();
}

?

On 17 November 2014 10:55, Vivek Anand <[email protected]> wrote:

> To override functionality of a factory, you need to create another class and 
> set the prototype as new FactoryClass. Following code change will do the 
> magic and it works like a charm. :)
>
>
> function MyCtrl($scope, MyObject, MyObjectWithParam) {
>
>     var NewObject = function () {
>         MyObject.apply(this, arguments);
>     };
>     NewObject.prototype = new MyObject();
>
>     NewObject.prototype.getText = function () {
>         return 'override';
>     }
>     MyObjectWithParam.prototype.getText = function () {
>         return 'override';
>     }
>     var obj1 = new NewObject(),
>         obj2 = new MyObjectWithParam("hello"),
>         obj3 = new MyObjectWithParam("world");
>
>     obj1.constructor.prototype.getText = function () {
>         return 'aa';
>     }
>
>     $scope.text = [
>         obj1.getText(),
>         obj2.getText(),
>         obj3.getText()
>     ];
> }
>
>  --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Tony Polinelli

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to