javascript - Is it possible to let Angularjs work with prototype methods and variables - Stack Overflow

You know, in angularjs, most of logical are based on $scope:function Ctrl($scope) {$scope.name = "

You know, in angularjs, most of logical are based on $scope:

function Ctrl($scope) {
    $scope.name = "Freewind";
    $scope.hello = function() {
       alert($scope.name);
    }
    $scope.method1 = function() {}
    $scope.method2 = function() {}
    $scope.method3 = function() {}
    $scope.method4 = function() {}
    $scope.method5 = function() {}
}

Now I'm using haxe to generate angularjs code, it works if my code is:

class Ctrl {
   public function new(scope:Scope) {
      scope.name = "Freewind";
      scope.hello = function() {
         alert(scope.name);
      }
      scope.method1 = function() {}
      scope.method2 = function() {}
      scope.method3 = function() {}
      scope.method4 = function() {}
      scope.method5 = function() {}
   }
}

typedef Scope = {
    name:String,
    hello:Void->Void,
    method1: Void->Void,
    method2: Void->Void,
    method3: Void->Void,
    method4: Void->Void,
    method5: Void->Void
}

But I want to be benefited from haxe's class system(the code may be simpler and clearer), to declare it like:

class Scope {
    public var name:String;
    public function hello() {}
    public function method1() {}
    public function method2() {}
    public function method3() {}
    public function method4() {}
    public function method5() {}
}

Then find a way to associate the Scope class with the $scope of angularjs.

But the generated Scope from haxe are using prototypes:

Scope = function();
Scope.prototype.name = "something";
Scope.prototype.hello = function() {}
Scope.prototype.method1 = function() {}
Scope.prototype.method2 = function() {}
Scope.prototype.method3 = function() {}
Scope.prototype.method4 = function() {}
Scope.prototype.method5 = function() {}

In this case, I can't find a solution to let angularjs work with it.

Is it possible to let angularjs to work with prototypes, so it can work with haxe class system (also other languages like coffeescript/typescript which have class support)?

You know, in angularjs, most of logical are based on $scope:

function Ctrl($scope) {
    $scope.name = "Freewind";
    $scope.hello = function() {
       alert($scope.name);
    }
    $scope.method1 = function() {}
    $scope.method2 = function() {}
    $scope.method3 = function() {}
    $scope.method4 = function() {}
    $scope.method5 = function() {}
}

Now I'm using haxe to generate angularjs code, it works if my code is:

class Ctrl {
   public function new(scope:Scope) {
      scope.name = "Freewind";
      scope.hello = function() {
         alert(scope.name);
      }
      scope.method1 = function() {}
      scope.method2 = function() {}
      scope.method3 = function() {}
      scope.method4 = function() {}
      scope.method5 = function() {}
   }
}

typedef Scope = {
    name:String,
    hello:Void->Void,
    method1: Void->Void,
    method2: Void->Void,
    method3: Void->Void,
    method4: Void->Void,
    method5: Void->Void
}

But I want to be benefited from haxe's class system(the code may be simpler and clearer), to declare it like:

class Scope {
    public var name:String;
    public function hello() {}
    public function method1() {}
    public function method2() {}
    public function method3() {}
    public function method4() {}
    public function method5() {}
}

Then find a way to associate the Scope class with the $scope of angularjs.

But the generated Scope from haxe are using prototypes:

Scope = function();
Scope.prototype.name = "something";
Scope.prototype.hello = function() {}
Scope.prototype.method1 = function() {}
Scope.prototype.method2 = function() {}
Scope.prototype.method3 = function() {}
Scope.prototype.method4 = function() {}
Scope.prototype.method5 = function() {}

In this case, I can't find a solution to let angularjs work with it.

Is it possible to let angularjs to work with prototypes, so it can work with haxe class system (also other languages like coffeescript/typescript which have class support)?

Share Improve this question edited Jan 18, 2013 at 11:20 Freewind asked Jan 18, 2013 at 10:57 FreewindFreewind 199k163 gold badges452 silver badges736 bronze badges 1
  • there is no purpose doing that , if you want reusable bits of code , move your methods into services. What angularJS calls controler is not the MVC controller , it is a ViewModel in the MVVM design pattern. true controllers are in directives. – mpm Commented Jan 18, 2013 at 11:10
Add a ment  | 

2 Answers 2

Reset to default 2

For the sake of having an actual answer to this question, one should mention that now this library solves the problem: https://github./freewind/HaxeAngularSupport ;)

Scope's constructor is declared within a closure, so you don't have easy access to it... BUT(!) JavaScript has the constructor available to you right off of any existing object.

Theoretically, you could get the $rootScope's constructor, alter it's prototype, and that should alter any subsequently created Scopes. You'd probably want to do this in the .run() or .config() on your application module, however.

app.run(function($rootScope) {
   $rootScope.constructor.prototype.foo = 'Set from prototype';
});

It works and here's the plunker.

HOWEVER: You probably don't need to do this. The only reason I could think of that you might need to do this is in some edge-case where you needed to make sure some function or property was available on a scope, even if it was an isolated scope (as was the one in the directive in the plunker I linked).

Because there is scope inheritance, and because you have $rootScope to use, I can't really think of any valid reason to need to alter Scope via prototyping.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744898281a4599828.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信