简体   繁体   English

使用“ controller as”语法时,如何访问控制器中的“ this”?

[英]How can I access the “this” in my controller when using “the controller as” syntax?

I have the following code: 我有以下代码:

var app = angular.module('plunker', []);

app.controller('ParentCtrl', function($scope) {
  $scope.name1 = 'Parent';
  this.name1 = 'Parent';
});

app.controller('ChildCtrl', function($scope) {
  $scope.name1 = 'Child';
  this.name1 = 'Child';
  this.option = {};
  this.option.abc = 25;

  foo = function(){
    alert(this.option)
  };

  foo();
});

When I try to access the "this" before the function it is available. 当我尝试使用该功能之前的“ this”时。 When I try to access the this inside of the function then it's undefined. 当我尝试在函数内部访问this时,它是未定义的。 Can someone tell me is there an "AngularJS" way to resolve this? 有人可以告诉我是否有一种“ AngularJS”方法可以解决此问题?

If you're using Controller As Syntax then declarations like: 如果您使用Controller As Syntax,则声明如下:

$scope.name1 = 'Child';
this.name1 = 'Child'; 

are redundant. 是多余的。 When you specify the 'As' angular will copy over all properties defined on this, to a $scope variable. 当您指定'As'角时,它将在此定义的所有属性上复制到$ scope变量。

as for why this doesn't work in your function. 至于为什么this在您的功能中不起作用。 The this keyword refers to the executing context of the function. this关键字是指函数的执行上下文。 As you have declared it there is no context. 正如您已经声明的那样,没有上下文。 To get this to be what you expect it you can do: 为了得到this是你希望它是什么,你可以这样做:

this.foo = function(){...this is this...}

or if you don't wish to expose it on the scope. 或者如果您不想在示波器上显示它。 You can use the module revealing pattern. 您可以使用模块显示模式。

var self = this;
function foo(){...self is 'this'...};

You need to keep the reference of this at the start of the function and use that. 您需要在函数开始时保留对它的引用并使用它。 The context of this i believe in your function is the global window. 的情况下this我相信在您的函数是全局窗口。 It all depends upon how the function is invoked and where is it defined. 这完全取决于函数的调用方式以及在何处定义。

app.controller('ChildCtrl', function($scope) {
  var self=this;
  $scope.name1 = 'Child';
  this.name1 = 'Child';
  this.option = {};
  this.option.abc = 25;

  foo = function(){
    alert(self.option)
  };

  foo();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用Typescript时,如何从子控制器访问父控制器中的作用域函数? - How can I access a scope function in a parent controller from a child controller when using Typescript? 如何在MVC控制器中访问JavaScript数组? - How can I access a JavaScript array in my MVC controller? 使用“作为控制器”语法时,如何在子控制器中引用父级的作用域? - How do I refer to the scope of a parent inside a child controller when using the “as controller” syntax? 如何从调用控制器的指令中访问控制器的属性 - How can I access attribute of controller in directive from calling controller 处理“ Controller As”语法时,如何访问隔离范围API字段? - How to access Isolated scope API fields when dealing with `Controller As` syntax? 如何在角度指令中访问控制器 - How can I access controller in angular directive 如何在SAP UI中的控制器中定义自定义控制器? - How i can define custom controller in my controller in sap ui? 无法使用UI路由器和“ controller as”语法访问视图中的控制器属性 - Can't access controller properties in view with UI router and 'controller as' syntax 如何在控制器中测试功能? - how can I test functions in my controller? 使用控制器作为语法时,如何从父指令继承属性? - How do I inherit properties from parent directive when using the controller as syntax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM