简体   繁体   English

使用这个。 在角不起作用?

[英]Using this. in angular does not work?

I created a simple button using angularjs. 我使用angularjs创建了一个简单的按钮。 So, the code in HTML in the views looks like this : 因此,视图中HTML中的代码如下所示:

    <div ng-controller="ButtonCtrl">
     <button ng-click="ButtonCtrl.setIndex(1)">Create another bidding query</button>
      <button ng-click="ButtonCtrl.setIndex(2)">Create another asking query</button>
        <form  ng-hide="ButtonCtrl.isSelected(1)">
         <h4>Filling The Bidding Form</h4>
         <fieldset class="form-group">
          <textarea class="form-control" ></textarea>
         </fieldset>
         </form>
       <div>

And the ButtonCtrl is defined as follows in the controller.js 并且ButtonCtrl在controller.js中定义如下

app.controller('ButtonCtrl', function($scope) {
   this.index=0;
   this.setIndex=function(setbutt){
       this.index=setbutt;
       };

    this.isSelected=function(checkbutt){
        return this.index===checkbutt;
    };
});

However, I do not get the expected behavior. 但是,我没有得到预期的行为。 The form does not hide itself when I click Create another bidding query button. 当我单击创建另一个出价查询按钮时,表单不会隐藏自身。 When I use $scope function to replace the variable, such as $scope.index=0;, the program works. 当我使用$ scope函数替换变量,例如$ scope.index = 0;时,程序运行。

I don't think the problem lies with using this.index because it works well for my other program. 我不认为问题出在使用this.index,因为它对我的其他程序很有效。 So, what is the exact problem? 那么,确切的问题是什么?

To be able to do this, you will have to use controllerAs syntax for using controllers in html. 为了做到这一点,您将必须使用controllerAs语法在html中使用控制器。

<div ng-controller="ButtonCtrl as buttonCtrl">
 <button ng-click="buttonCtrl.setIndex(1)">Create another bidding query</button>
  <button ng-click="buttonCtrl.setIndex(2)">Create another asking query</button>
    <form  ng-hide="buttonCtrl.isSelected(1)">
     <h4>Filling The Bidding Form</h4>
     <fieldset class="form-group">
      <textarea class="form-control" ></textarea>
     </fieldset>
     </form>
   <div>

And now, if use this in your controller instead of $scope, everything will work fine. 现在,如果在控制器中使用它而不是$ scope,一切都会正常。 See the example here 看到这里的例子

But personally I prefer using $scope inside controllers. 但是我个人更喜欢在控制器内部使用$ scope。 It feels more natural to me, but that's just a personal choice. 对我来说,这很自然,但这只是个人选择。

If you want to be able to use your syntax ButtonCtrl.setIndex(1) you have to assign this to $scope in your controller like in the last line here: 如果你希望能够使用你的语法ButtonCtrl.setIndex(1)你必须分配this$scope在控制器也喜欢在这里的最后一行:

app.controller('ButtonCtrl', function($scope) {
   this.index=0;
   this.setIndex=function(setbutt){
       this.index=setbutt;
       };

    this.isSelected=function(checkbutt){
        return this.index===checkbutt;
    };

    // set ButtonCtrl to the $scope
    $scope.ButtonCtrl = this;
});

Here is a good egghead.io video which explains it: An Alternative Approach to Controllers . 这是一个很好的egghead.io视频,解释了它: 控制器的替代方法

But as other answers already pointed out. 但是正如其他答案已经指出的那样。 This is not really idiomatic usage of AngularJS. 这并不是AngularJS的惯用用法。

Assuming you just want to get your sample code working, there are a few things I notice. 假设您只是想让示例代码正常工作,我注意到了一些事情。 First of all, most Angular code uses the $scope in place of this so that you can leverage the scope features that Angular provides. 首先, 大多数 Angular代码使用$scope代替它,以便您可以利用Angular提供的范围功能。 In fact $scope is meant to be used instead of this . 实际上,应该使用$scope 代替 this So, in terms of following the Angular way, the controller would look like this: 因此,就遵循Angular方式而言,控制器将如下所示:

app.controller('ButtonCtrl', function($scope) {
   $scope.index=0;
   $scope.setIndex=function(setbutt){
       $scope.index=setbutt;
       };

    $scope.isSelected=function(checkbutt){
        return $scope.index===checkbutt;
    };
});

Further, once you have declared a controller (in the markup), you don't need to prefix each method call. 此外,一旦在标记中声明了一个控制器,就不需要为每个方法调用加上前缀。 This is one of the benefits of using $scope correctly. 这是正确使用$scope的好处之一。 So, the markup would change to something like this: 因此,标记将变为如下所示:

<div ng-controller="ButtonCtrl">
     <button ng-click="setIndex(1)">Create another bidding query</button>
     <button ng-click="setIndex(2)">Create another asking query</button>
         <form  ng-hide="isSelected(1)">
         <h4>Filling The Bidding Form</h4>
         <fieldset class="form-group">
             <textarea class="form-control" ></textarea>
         </fieldset>
     </form>
<div>

Hope this helps. 希望这可以帮助。

You should be using $scope or just variables in scope instead of this . 您应该在范围内使用$scope或仅使用变量,而不要使用this

Like so: 像这样:

$scope.index = 0;
$scope.setIndex = function(i) {
    $scope.index = i;
};
$scope.isSelected = function(i) {
    return ($scope.index === i);
};

(Or for example var index = 0; instead, if for some reason you don't want it in scope). (或者例如, var index = 0;相反,如果由于某种原因您不希望它在范围内)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM