Created
June 18, 2015 07:23
-
-
Save digitalkaoz/871868df09aaf45120b8 to your computer and use it in GitHub Desktop.
Angular with Typescript - the React-Component-Style way...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import angular = require('angular'); | |
import ModuleA from './components/moduleA/index'; | |
class App { | |
name = 'myapp'; | |
selector = '[ng-app]'; | |
private mod: angular.IModule; | |
constructor() { | |
var dependencies: string[] = []; | |
dependencies.push(ModuleA); | |
this.mod = angular.module(this.name, dependencies); | |
} | |
} | |
(() => { | |
var app = new App(); | |
angular | |
.bootstrap(document.querySelector(app.selector), [app.name], { | |
strictDi: true | |
}); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//components/moduleA/index.ts | |
import SomeService from './SomeService'; | |
import SomeComponent from './SomeComponent'; | |
class ModuleA { | |
name = 'myapp.moduleA'; | |
private mod = angular.module(this.name, []); | |
constructor() { | |
this.mod.service('someService', SomeService); | |
this.mod.directive('someComponent', SomeComponent); | |
this.mod.filter('urlDecode', [() => { | |
return decodeURIComponent; | |
}]); | |
} | |
} | |
export default ((): string => { | |
var module = new ModuleA(); | |
return module.name; | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div ng-app="myapp"> | |
<someComponent model="{some:'data'}"></someComponent> | |
</div> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//components/moduleA/SomeComponent.ts | |
class Controller { | |
model:any; | |
// @ngInject | |
constructor(private someService:SomeService, private $scope:angular.IScope) { | |
} | |
save() { | |
alert('lolcat'); | |
} | |
static template = `<div>{{ someComponent.model }}<a ng-click="someComponent.save()">Click</a></div>` | |
} | |
export default ():angular.IDirective => { | |
return { | |
restrict: 'E', | |
bindToController: { | |
model: '@', | |
}, | |
scope: true, | |
controllerAs: 'someComponent', | |
controller: Controller, | |
template: Controller.template | |
//templateUrl: 'components/moduleA/template.html' //if used with angular template cache compilation | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
credits goes to @akoenig ;)