0

I've a custom angular filter directive which can replace strings using the following syntax:

{{ 'MyValue' | replace: 'My':'Foo' }}

This works fine. Now I want to use this filter inside a ng-click

<button ng-click="alert('{{ 'MyValue' | replace: 'My':'Value'}}')">Click Me!</button>

This throws an parse exception (I assume because of the surrounding apostrophes from the alert). What's the correct syntax in this case?

A Plunkr is available here: http://plnkr.co/edit/xEGjY40LWORt5nlaJq46

2
  • in your plunkr, the alert is never fired (if i replace the code with normal text). I'm not sure, but it could be because you don't have a controller? Commented Dec 11, 2014 at 9:30
  • The problem is this parse exception Commented Dec 11, 2014 at 9:32

2 Answers 2

1

here is the working Plunker

i added the controller and call a function inside controller when ng-click fired

 <body ng-app="MyApp" ng-controller="x">
   <p>{{ -3 | replace: '-':'n' }}</p>
   <p>
     <button ng-click="x('MyValue','replace','My','Value')">Click Me!</button>
  </p>
 </body>

in script.js

myApp.controller('x', function($scope, $filter) {
  $scope.x = function(input,filterName,replaceStr,replaceBy) {
      var filtered = $filter(filterName)(input,replaceStr,replaceBy);
      alert(filtered);      
  }
});

for the more

ng-click='alert("1111")' dose not work because the when u put something inside ng-click, alert() in this case, it will check for $scope.alert so, alertis not work insideng-click`

Sign up to request clarification or add additional context in comments.

Comments

1

As a rule, you cannot use interpolated values as a parameter to an executable attribute in angular ('&').

what you need to do is:

markup:

<button ng-click="alert(customValue)">Click Me!</button>

Controller code

$scope.customValue = $fitler('replace')('MyValue', 'My', 'Value');

Comments

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.