1

我已经使用 typescript 创建了 angularjs 1.5 组件,但它不起作用,当我说它不起作用时,它的绑定根本不起作用。当它触发 $onInit 函数时,我可以在控制台中看到数据,但它永远不会将任何数据放在屏幕上。这是我第一次使用打字稿,如果有人能指出我正确的方向,我会非常感激?

我的组件代码如下

class Users implements ng.IComponentOptions {
    public bindings: any;
    public controller: any;
    public template: string;
    public templateUrl: string;
    public transclude: boolean;
    public controllerAs: string;

    constructor() {
        this.controller = UsersController;
        this.templateUrl = "Template/Index?feature=users&template=users";
        this.transclude = false;
        this.controllerAs = "vm";
    }
}


class UsersController {
    public name: string;
    public test: string;
    public Users: any[];

    static $inject: string[] = ["$http", "repository.user"];

    constructor(private $http: any, private RepositoryUser: any) {
    }

    public $onInit(): void {
        this.RepositoryUser.getUsers().then(function (response) {
            this.Users = response.data;
            console.dir(this.Users);
        });
    }

    public setUser(id: number, value: any): void {
        this.Users[this.Users.indexOf(value)] = value;

    }
}

angular.module("app.users").component("users", new Users());

我的html是

<div class="row">
    <div class="col-sm-12">
        <ng-form>
            <h2>Users</h2>

            <div ng-repeat="model in vm.Users track by $index" class="row top10">
                <div class="col-sm-8">
                    <a ui-sref="user-details({ id: model.id })"><strong ng-bind="model.name"></strong></a>
                </div>
                <div class="col-sm-4">
                    <button class="btn btn-default" type="button" ng-click="vm.selectedId=model.id">Select</button>
                </div>
            </div>

        </ng-form>
    </div>
</div>
4

1 回答 1

0

尝试使用以下语法:https ://blogs.infinitesquare.com/posts/web/creer-un-component-angularjs-en-typescript 该帖子是法语的,但您可以阅读示例代码。

如果您有更多问题,请告诉我。

于 2017-03-29T07:53:30.430 回答