简体   繁体   English

如何在Angular JS中给$ this

[英]How ot give $this in Angular js

I am displaying datas in a grid ,on click of active or inactive button i have to change the button ,functionality is working fine ,for changing icon i am unable to find the active clicked button ,in jquery we can use " this " ,but no idea in angular js ,pls help 我在网格中显示数据,单击活动按钮或非活动按钮时,我必须更改按钮,功能正常,对于更改图标,我找不到活动的单击按钮,在jquery中我们可以使用“ this ”,但是在Angular JS中不知道,请帮助

 $scope.activeSource = function(datasource,status)
                {

                        $scope.loading = true;
                        $scope.activeInfo = {
                                datasource:datasource,
                        };
                        $scope.alerts = [];
                        if(status == "Stopped")
                        {
                            $scope.entityService.activeInfo($scope.activeInfo,
                            function( msg ) // success
                            {
                                $scope.loading = false;
                                $rootScope.successMsg.push(msg);
                                $('.icon-play').hide();  // this.
                                $('.icon-stop').show();  // this.  no idea

                            },
                            function( msg ) // error
                            {
                                $scope.loading = false;
                                $rootScope.successMsg.push(msg);
                            }
                        );

You don't need to use this to access the button. 您不需要使用this来访问按钮。 Instead, in your controller create a javascript object that holds all the attributes for the button. 而是在您的控制器中创建一个JavaScript对象,该对象包含按钮的所有属性。 Something like this: 像这样:

$scope.myButton = {
  src : 'foobar.png',
  isVisible : true
};

Then, define your button like this: 然后,像这样定义您的按钮:

<img ng-src="myButton.src" ng-show="myButton.isVisible" />

Now, when you want to modify any attribute of the button, you just need to change the javascript object myButton , and angular will take care of updating the actual button. 现在,当您想要修改按钮的任何属性时,只需更改javascript对象myButton ,angular将负责更新实际按钮。

For example, if you want to hide the button: 例如,如果要隐藏按钮:

// you don't need to do this
// $('#myButton').hide();

// instead just do this
$scope.myButton.isVisible = false;

Similarly, you can change the src of the image as well. 同样,您也可以更改图像的src。

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

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