Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.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/5/reporting-services/3.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 类的角度绑定_Javascript_Angularjs_Typescript_Angularjs Directive - Fatal编程技术网

Javascript 类的角度绑定

Javascript 类的角度绑定,javascript,angularjs,typescript,angularjs-directive,Javascript,Angularjs,Typescript,Angularjs Directive,es6类给我带来了麻烦,我在任何地方都找不到使用它的文档。 我有这个控制器 class AController { conversation: any; constructor() { 'ngInject'; } $onInit() { console.log(this.conversation) // This is undefined. } } 我有这个组件 export const AComponent = function () { retu

es6类给我带来了麻烦,我在任何地方都找不到使用它的文档。 我有这个控制器

class AController {
  conversation: any;

  constructor() {
    'ngInject';
  }

  $onInit() {
    console.log(this.conversation) // This is undefined.
  }
}
我有这个组件

export const AComponent = function () {
  return {
    template: require('./attachments.component.jade'),
    bindings: {
      conversation: '<'
    },
    controller: AController,
    controllerAs: '$ctrl'
  };
}

模板和控制器正在工作,但此对话未定义。无法找出哪里出了问题。

看起来一切正常,只是
这个。调用
$onInit
时可能无法定义对话


您可以检查
conversation
是否在
$onChanges
中定义,或者使用更高级别的抽象,如并检查
this.props.conversation
是否在其
render()
方法中定义。

使用
$onChanges
生命周期挂钩查看单向绑定中的值:

class AController {
  conversation: any;

  constructor() {
    'ngInject';
  }

  $onInit() {
    //console.log(this.conversation) // This is undefined.
  }

  $onChanges(changes) {
      if (changes.conversation) {
          console.log(changes.conversation.currentValue);
      }
  }
}
生命周期挂钩

  • $onChanges(changesObj)
    -每当单向(
    该模板的控制器使用$scope,因此
    $ctrl.conversation
    不起作用看起来像TypeScript,而不是ES6?很抱歉,您是对的。更改了它。有关使用ES6的AngularJS的文档,请参阅,您试图传递的属性值
    'conversation'
    是来自某个控制器作用域的字符串或变量吗?如果您不是尝试传递字符串时,请使用引号将其包装,或将绑定改为
    @
    ,而不是
    
    
    class AController {
      conversation: any;
    
      constructor() {
        'ngInject';
      }
    
      $onInit() {
        //console.log(this.conversation) // This is undefined.
      }
    
      $onChanges(changes) {
          if (changes.conversation) {
              console.log(changes.conversation.currentValue);
          }
      }
    }