如何通知指令有关参数的更改

时间:2016-01-06 04:23:01

标签: angularjs typescript

我有使用变量作为参数的指令:

<dm-my-directive var="item" />

该指令使用此参数进行一些计算和显示信息。

item的值来自Web服务,并且在控制器中初始化item的值需要一些时间。当用户打开页面时,指令是使用typeof(item) == undefined创建的,然后item的值设置为来自Web服务的某些Object

我需要在指令中知道item的值已更改为重新计算并重绘指令。我在指令中使用$watch来观察item的值,但可能有更好的方法吗?

更新

以下是控制器的代码:

constructor(public $scope: any, 
            public itemResource: app.common.ItemResource) {

    itemResource.itemsByAutoincrementedId(this.itemTypePosition).then((curItem: app.domain.Item) => {
                this.Item = curItem;
            });
}

以下是该指令的代码:

constructor(public $scope:any) {

      $scope.$watch('item', () => {

         if(typeof($scope.item) === "undefined") return;

         // ... here is business loginc

0 个答案:

没有答案