我有使用变量作为参数的指令:
<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