Angularjs 如何在TypeScript中分配getter/setter函数对?
我用的是带角度的打字脚本。我想在控制器的作用域中添加一对函数,Angularjs 如何在TypeScript中分配getter/setter函数对?,angularjs,properties,typescript,setter,getter,Angularjs,Properties,Typescript,Setter,Getter,我用的是带角度的打字脚本。我想在控制器的作用域中添加一对函数,currentPath():string和currentPath(newValue:string):void,并禁止直接访问支持变量。我还想考虑这种“类似财产”的行为。因此,我添加了一个界面: interface Property<T> { (): T; (newValue: T): void; } 接口属性{ ():T; (新值:T):无效; } 然后尝试按如下方式配置我的作用域: interface
currentPath():string
和currentPath(newValue:string):void
,并禁止直接访问支持变量。我还想考虑这种“类似财产”的行为。因此,我添加了一个界面:
interface Property<T> {
(): T;
(newValue: T): void;
}
接口属性{
():T;
(新值:T):无效;
}
然后尝试按如下方式配置我的作用域:
interface ApplicationRootScope extends ng.IScope {
currentPath: Property<string>;
}
appControllers.controller('MyCtrl', ($scope: ApplicationRootScope) => {
var _currentPath = "n/a";
$scope.currentPath = { // this assignment fails
(): string = _currentPath;
(newValue: string) => {
_currentPath = newValue;
}
};
});
接口应用程序根范围扩展了ng.IScope{
当前路径:属性;
}
appControllers.controller('MyCtrl',($scope:ApplicationRootScope)=>{
var\u currentPath=“不适用”;
$scope.currentPath={//此分配失败
():字符串=_currentPath;
(newValue:string)=>{
_currentPath=newValue;
}
};
});
标记行的赋值失败-我故意使用错误的语法来演示我想做什么。我有没有办法像这样直接分配
currentPath
变量?函数重载只是一种类型系统功能;JavaScript/TypeScript不直接支持函数arity的重载
你想写的是:
$scope.currentPath = function(arg?: string) {
if(arguments.length === 0) {
return _currentPath;
} else {
return _currentPath = arg;
}
}