Angularjs with Coffeescript class

I tried using angularjs with the Coffescript class

I was able to enter and make a successful example using coffeescript. But to access $ scope I have to write functions inside the constructor. What can I do to get rid of this. If there is another good way to write, please let me know.

Here is my coffeescript working code

class PersonCtrl @$inject = ['$scope'] constructor: (@scope) -> @scope.persons = [ firstName:"Kunjan" lastName:"Dalal" , firstName:"Kunj" lastName:"Dalal" ] @scope.addPerson = () => @scope.persons.push angular.copy @scope.person 

Please let me know if any further information is required.

+6
source share
1 answer

I used the following syntax:

 app = angular.module 'myapp', [] class MySimpleCtrl @$inject: ['$scope'] constructor: (@scope) -> @scope.demo = 'demo value' @scope.clearText = @clearText clearText: => @scope.demo = "" app.controller 'MySimpleCtrl', MySimpleCtrl angular.bootstrap document, ['myapp'] 

Take a look at this jsFiddle: http://jsfiddle.net/jwcMA/

UPDATE @ oto-brglez calling .bootstrap () replaces the presence of an ng application in the <html>

UPDATE @TylerCollier, this was a while ago, now I would probably use the controller as a notation (or maybe TypeScript!)

COFFEE

 class PetController constructor: (@$scope) -> @pets = ['Fido','Felix'] addPet: (pet) -> @pets.push(pet) 

HTML

 <div ng-controller="PetController as pc"> ... <li ng-repeat="pet in pc.pets"> 
+11
source

Source: https://habr.com/ru/post/951543/


All Articles