kpg*_*kpg 36 angularjs typescript angularjs-components
我正在试验.component()
Angular 1.5中的语法.
似乎最新的方式是在组件中对控制器进行内联编码,而不是在单独的文件中编码,并且我可以看到组件样板文件最小的优点.
问题是我将控制器编码为打字稿类,并希望继续这样做,因为这似乎与Angular2一致.
我最大的努力是这样的:
export let myComponent = {
template: ($element, $attrs) => {
return [
`<my-html>Bla</my-html>`
].join('')
},
controller: MyController
};
class MyController {
}
Run Code Online (Sandbox Code Playgroud)
它有效,但它并不优雅.有没有更好的办法?
sca*_*rlz 35
如果您想完全采用Angular 2方法,可以使用:
module.ts
import { MyComponent } from './MyComponent';
angular.module('myModule', [])
.component('myComponent', MyComponent);
Run Code Online (Sandbox Code Playgroud)
MyComponent.ts
import { Component } from './decorators';
@Component({
bindings: {
prop: '<'
},
template: '<p>{{$ctrl.prop}}</p>'
})
export class MyComponent {
prop: string;
constructor(private $q: ng.IQService) {}
$onInit() {
// do something with this.prop or this.$q upon initialization
}
}
Run Code Online (Sandbox Code Playgroud)
decorators.ts
/// <reference path="../typings/angularjs/angular.d.ts" />
export const Component = (options: ng.IComponentOptions) => {
return controller => angular.extend(options, { controller });
};
Run Code Online (Sandbox Code Playgroud)
Ali*_*our 32
我使用一个简单的Typescript装饰器来创建组件
function Component(moduleOrName: string | ng.IModule, selector: string, options: {
controllerAs?: string,
template?: string,
templateUrl?: string
}) {
return (controller: Function) => {
var module = typeof moduleOrName === "string"
? angular.module(moduleOrName)
: moduleOrName;
module.component(selector, angular.extend(options, { controller: controller }));
}
}
Run Code Online (Sandbox Code Playgroud)
所以我可以像这样使用它
@Component(app, 'testComponent', {
controllerAs: 'ct',
template: `
<pre>{{ct}}</pre>
<div>
<input type="text" ng-model="ct.count">
<button type="button" ng-click="ct.decrement();">-</button>
<button type="button" ng-click="ct.increment();">+</button>
</div>
`
})
class CounterTest {
count = 0;
increment() {
this.count++;
}
decrement() {
this.count--;
}
}
Run Code Online (Sandbox Code Playgroud)
你可以在http://jsbin.com/jipacoxeki/edit?html,js,output尝试一下工作的jsbin
rom*_*iem 13
这是我使用的模式:
ZippyComponent.ts
import {ZippyController} from './ZippyController';
export class ZippyComponent implements ng.IComponentOptions {
public bindings: {
bungle: '<',
george: '<'
};
public transclude: boolean = false;
public controller: Function = ZippyController;
public controllerAs: string = 'vm';
public template: string = require('./Zippy.html');
}
Run Code Online (Sandbox Code Playgroud)
ZippyController.ts
export class ZippyController {
bungle: string;
george: Array<number>;
static $inject = ['$timeout'];
constructor (private $timeout: ng.ITimeoutService) {
}
}
Run Code Online (Sandbox Code Playgroud)
Zippy.html
<div class="zippy">
{{vm.bungle}}
<span ng-repeat="item in vm.george">{{item}}</span>
</div>
Run Code Online (Sandbox Code Playgroud)
main.ts
import {ZippyComponent} from './components/Zippy/ZippyComponent';
angular.module('my.app', [])
.component('myZippy', new ZippyComponent());
Run Code Online (Sandbox Code Playgroud)
我正在努力解决同样的问题并将我的解决方案放在本文中:
http://almerosteyn.github.io/2016/02/angular15-component-typescript
module app.directives {
interface ISomeComponentBindings {
textBinding: string;
dataBinding: number;
functionBinding: () => any;
}
interface ISomeComponentController extends ISomeComponentBindings {
add(): void;
}
class SomeComponentController implements ISomeComponentController {
public textBinding: string;
public dataBinding: number;
public functionBinding: () => any;
constructor() {
this.textBinding = '';
this.dataBinding = 0;
}
add(): void {
this.functionBinding();
}
}
class SomeComponent implements ng.IComponentOptions {
public bindings: any;
public controller: any;
public templateUrl: string;
constructor() {
this.bindings = {
textBinding: '@',
dataBinding: '<',
functionBinding: '&'
};
this.controller = SomeComponentController;
this.templateUrl = 'some-component.html';
}
}
angular.module('appModule').component('someComponent', new SomeComponent());
}
Run Code Online (Sandbox Code Playgroud)
我正在使用以下模式来使用带有打字稿的angular 1.5 组件
class MyComponent {
model: string;
onModelChange: Function;
/* @ngInject */
constructor() {
}
modelChanged() {
this.onModelChange(this.model);
}
}
angular.module('myApp')
.component('myComponent', {
templateUrl: 'model.html',
//template: `<div></div>`,
controller: MyComponent,
controllerAs: 'ctrl',
bindings: {
model: '<',
onModelChange: "&"
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28216 次 |
最近记录: |