如果ng-click中的方法声明

时间:2016-03-17 17:10:17

标签: javascript angularjs angularjs-ng-click

如果没有设置RoleName,我想执行createRole(),否则只要我点击按钮就可以执行EditRole()方法而不是EditRole()执行

<button type="submit" ng-click="selectedItem.RoleName? EditRole() : createRole()">Save Changes</button>

2 个答案:

答案 0 :(得分:2)

<button type="submit" ng-click="save()">Save Changes</button>

在控制器中:

 $scope.save = function(){

    if($scope.selectedItem.RoleName){
        EditRole() 
    }
    else{
        createRole()
    }

 }

答案 1 :(得分:0)

可能更好的解决方案是

<button type="submit" ng-click="check(selectedItem.RoleName)">Save Changes</button>

$scope.check=function(role){
if(role){ // exist
EditRole();
}
else{
createRole(); // if role name is not set
}
}