Angularjs 如何访问typescript控制器类中的typescript指令隔离作用域值?
我有一个Typescript指令类和一个控制器类,如下所示。我想在Typescript控制器类中的隔离作用域变量上设置一个监视。我无法访问控制器类中的隔离作用域变量 我该怎么做 我的指令类: 模块Sgm.Workspace.Web.Users{ 严格使用; 导出类权限实现ng.IDirective{ 限制=E; 替换=假; templateUrl=/app/features/users/uwAuthority.html; 范围={ showParameters:'=showParameters' }; 控制器=UwAuthorityController; //controllerAs=vm; 链接范围:ng.IScope,元素:ng.IAugmentedJQuery,属性:ng.IAttributes,控制器:UserParameters控制器:void{ } 静态实例:ng.IDirective{ 归还新的授权; } } angular.moduleworkspaceApp .directiveshowwauthority,UwAuthority.instance .controllerUwAuthorityController,['userService','$scope',userService:Sgm.Workspace.Web.Services.IUserService,$scope:ng.IScope=>new-UwAuthorityController,$scope];Angularjs 如何访问typescript控制器类中的typescript指令隔离作用域值?,angularjs,typescript,Angularjs,Typescript,我有一个Typescript指令类和一个控制器类,如下所示。我想在Typescript控制器类中的隔离作用域变量上设置一个监视。我无法访问控制器类中的隔离作用域变量 我该怎么做 我的指令类: 模块Sgm.Workspace.Web.Users{ 严格使用; 导出类权限实现ng.IDirective{ 限制=E; 替换=假; templateUrl=/app/features/users/uwAuthority.html; 范围={ showParameters:'=showParameters
} 我们可以为隔离作用域创建接口,并将其传递给控制器:
// the custom scope interface
export interface IUwAuthorityScope extends ng.IScope
{
showParameters: boolean;
}
// the controller interface
export interface IUwAuthorityController
{
loadFinancingType(): void;
}
export class UwAuthorityController implements IUwAuthorityController
{
// here we inform compiler, that injected '$scope' will be of
// our custom type - IUwAuthorityScope
constructor(
private userService: Services.IUserService,
private $scope: IUwAuthorityScope)
{
...
// here we can watch whatever we expect to be in the $scope
this.$scope.$watch("showParameters", showParms => {
// here we can access fully type $scope variables
if(this.$scope.showParameters === true) ...
});
}
...
我们可以为隔离作用域创建接口,并将其传递给控制器:
// the custom scope interface
export interface IUwAuthorityScope extends ng.IScope
{
showParameters: boolean;
}
// the controller interface
export interface IUwAuthorityController
{
loadFinancingType(): void;
}
export class UwAuthorityController implements IUwAuthorityController
{
// here we inform compiler, that injected '$scope' will be of
// our custom type - IUwAuthorityScope
constructor(
private userService: Services.IUserService,
private $scope: IUwAuthorityScope)
{
...
// here we can watch whatever we expect to be in the $scope
this.$scope.$watch("showParameters", showParms => {
// here we can access fully type $scope variables
if(this.$scope.showParameters === true) ...
});
}
...
我确实听从了你的建议,但我一直有这样的错误:
错误:[$injector:unpr]未知提供程序:IuwaAuthorityScopeProvider我确实按照您的建议执行了此操作,但我一直出现此错误:
错误:[$injector:unpr]未知提供程序:IuwaAuthorityScopeProvider不用于TypeScript,但showParameters在作用域上,而不是在控制器上,因此可能是这个。$scope。$watch=>$scope.showParameters或这个。$scope。$watch'showParameters。如果您使用的是angular v1.3或更高版本,您可以在指令上使用bindToController选项,该选项将指令的作用域与控制器关联。不用于键入脚本,但showParameters位于作用域上,而不是控制器上,因此,它可能是这个。$scope。$watch=>$scope.showParameters或这个。$scope。$watch'showParameters'。如果您使用的是angular v1.3或更高版本,您可以在指令上使用bindToController选项,该选项将指令的范围与控制器相关联。您可以使用类似的。$scope[showParameters]。属性。它可以工作。您可以使用这样的内容。$scope[showParameters]。属性。它起作用了