1

var app=angular.module('ang',[]);
app.controller('ctr',function($scope){

$scope.run=function(){alert("run")}
$scope.break=function(){alert("break")}

})
.test{width:160px;height:30px;background-color:green;color:white}
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  <body>

    <div ng-app="ang" ng-controller='ctr'>
 
      <button class="test" ng-click="a?'run()':'break()'">click=calling...{{a?'run()':'break()'}}</button>
    <button ng-click="a=!a">click me</button>{{a}}

    </div>

  </body>
</html>

Here i want to dynamically change the value of ng-click based on a value using conditional statement,but it is not working as expected.Please help me out Thanks in advance

1

2 Answers 2

4

Change this line shown as below

Because it is already interpolated. You need not to mention ' ' again to call the scope function

<button class="test" ng-click="a?run():break()">click=calling...{{a?'run()':'break()'}}</button>
Sign up to request clarification or add additional context in comments.

Comments

1

remove the single quote in the function

change this

{{a?'run()':'break()'}}

to this

{{a?run():break()}}

var app=angular.module('ang',[]);
app.controller('ctr',function($scope){

$scope.run=function(){alert("run")}
$scope.break=function(){alert("break")}

})
.test{width:160px;height:30px;background-color:green;color:white}
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  <body>

    <div ng-app="ang" ng-controller='ctr'>
 
      <div class="test" ng-click="a?'run()':'break()'">click=calling....{{a?run():break()}}</div>
    <button ng-click="a=!a">click me</button>{{a}}

    </div>

  </body>
</html>

2 Comments

OHHH,Thank yOU verymuch :)
@Abhishek no problem make sure to check as answer if this helped :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.