Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用bindToController时,构造函数中未定义指令的作用域变量_Javascript_Angularjs_Angularjs Directive_Typescript - Fatal编程技术网

Javascript 使用bindToController时,构造函数中未定义指令的作用域变量

Javascript 使用bindToController时,构造函数中未定义指令的作用域变量,javascript,angularjs,angularjs-directive,typescript,Javascript,Angularjs,Angularjs Directive,Typescript,所以我有一个指令,它在创建时应该有一个值。让我们调用指令MyDirective。要使用它并向其传递值,可以执行以下操作: <my-directive value="'I will be undefined'"></my-directive> 因此,问题是我需要使用$watch,因为传递给指令的值(“我将未定义”)在构造函数中(我需要它的地方…)时尚未设置 没有手表有更好的方法吗?有 我刚刚对您的代码进行了一些调整,它正在工作: namespace MyNamespace

所以我有一个指令,它在创建时应该有一个值。让我们调用指令
MyDirective
。要使用它并向其传递值,可以执行以下操作:

<my-directive value="'I will be undefined'"></my-directive>
因此,问题是我需要使用
$watch
,因为传递给指令的值(“我将未定义”)在构造函数中(我需要它的地方…)时尚未设置

没有手表有更好的方法吗?

我刚刚对您的代码进行了一些调整,它正在工作:

namespace MyNamespace { 

    export class MyDirectiveController {
        public value:string;    
        static $inject = ['$scope'];

        constructor(private $scope: ng.IScope) {
            // I wanna do something with this.value at this point
            // NOW It is defined
            console.log(this.value);
            $scope.$watch('value', this.valueDidChangeCallback).bind(this);
        }

        valueDidChangeCallback:any = () => {        
            // Now I can do the thing I wanted to do ...
            console.log(this.value);
        };
      }

    export class MyDirectiveDirective {
        restrict: string = 'E';
        templateUrl: string = 'my-directive.html';
          controller = MyNamespace.MyDirectiveController;
          controllerAs: string = 'vm';
        bindToController: boolean = true;    
        scope:any = {
            'value': '='
        };
      }

    angular
       .module('MyApp')
       .directive('myDirective', [() => {return new MyNamespace.MyDirectiveDirective()}])
}

我认为你把
controllerAs
bindToController
搞混了。ControllerAs只是将控制器作为变量分配给作用域。其中,bindToController将使用控制器作为作用域。因此,
this==$scope
是正确的。这可能是,我只是尝试使用不带
$scope
变量的类,并将一个值传递给指令,我无需手表就可以使用该指令,知道是否可以这样做吗?很高兴看到这一点!使用Angular;)享受打字脚本
namespace MyNamespace { 

    export class MyDirectiveController {
        public value:string;    
        static $inject = ['$scope'];

        constructor(private $scope: ng.IScope) {
            // I wanna do something with this.value at this point
            // NOW It is defined
            console.log(this.value);
            $scope.$watch('value', this.valueDidChangeCallback).bind(this);
        }

        valueDidChangeCallback:any = () => {        
            // Now I can do the thing I wanted to do ...
            console.log(this.value);
        };
      }

    export class MyDirectiveDirective {
        restrict: string = 'E';
        templateUrl: string = 'my-directive.html';
          controller = MyNamespace.MyDirectiveController;
          controllerAs: string = 'vm';
        bindToController: boolean = true;    
        scope:any = {
            'value': '='
        };
      }

    angular
       .module('MyApp')
       .directive('myDirective', [() => {return new MyNamespace.MyDirectiveDirective()}])
}