如何在function assigned variable
中使用 ng-click?
?我在下面尝试了三种方式,但这也无效。
var app = angular.module("app", []);
app.controller("name", function($scope) {
$scope.showname = function()
{
alert("Ramesh");
}
$scope.variable1 = $scope.showname;
$scope.variable2 = "showname()";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="name">
<button type="button" ng-click="variable1" tabindex="10003" >Not working 1 </button>
<button type="button" ng-click="$scope[variable2]()" tabindex="10003" >Not working 2 </button>
<button type="button" ng-click="variable2" tabindex="10003" >Not working 3 </button>
<button type="button" ng-click="showname()" tabindex="10003"> working </button>
</div>
答案 0 :(得分:0)
尝试:
<button type="button" ng-click="variable1()" tabindex="10003" >Now it'll work 1 </button>
答案 1 :(得分:0)
$scope.showname
有函数引用。所以只需将它分配给您的变量,如
$scope.variable1 = $scope.showname
然后拨打ng-click,如valiable1()
var app = angular.module("app", []);
app.controller("name", function($scope) {
$scope.showname = function()
{
alert("Ramesh");
}
$scope.variable1 = $scope.showname;
$scope.variable2 = $scope.showname;
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="name">
<button type="button" ng-click="variable1()" tabindex="10003" >Not working 1 </button>
<button type="button" ng-click="variable2()" tabindex="10003" >Not working 2 </button>
<button type="button" ng-click="variable1()" tabindex="10003" >Not working 3 </button>
<button type="button" ng-click="showname()" tabindex="10003"> working </button>
</div>
&#13;
答案 2 :(得分:0)
不使用$scope
,而是使用this
关键字:
误
<button type="button" ng-click="$scope[variable2]()" tabindex="10003" > Not working 2 </button>
改为使用:
<button type="button" ng-click="this[variable2]()" tabindex="10003" >
working
</button>
可以使用标识符this
访问上下文对象。