0

I want to have several things into one AngularJS expression but when use semicolon - ";" expression not working.

data-ng-click="secondTerm.keyphrase.selected=!secondTerm.keyphrase.selected; alert(1)"

How I can solve this problem?

Best regards.

Edit:

data-ng-click="secondTerm.keyphrase.selected=!secondTerm.keyphrase.selected;$window.alert(1)"

also don't work. Using of alert is just an example. I want to call other function after

secondTerm.keyphrase.selected=!secondTerm.keyphrase.selected
Georgi Naumov
  • 4,160
  • 4
  • 40
  • 62
  • possible duplicate of [Accessing the window from Angular expression](http://stackoverflow.com/questions/20998330/accessing-the-window-from-angular-expression) – Sergiu Paraschiv Jun 16 '14 at 08:26
  • 1
    `this` in an AngularJS expression is a `$scope`. `alert` is defined on `window`, it's not global. Read the duplicate question and answer. – Sergiu Paraschiv Jun 16 '14 at 08:27
  • data-ng-click="secondTerm.keyphrase.selected=!secondTerm.keyphrase.selected;$window.alert(1)" also don't work. alert(1) was only example. I just want to call other function instead alert. – Georgi Naumov Jun 16 '14 at 08:30
  • I'm pretty sure you did not read the answer carefully: `$scope.$window = $window;`. – Sergiu Paraschiv Jun 16 '14 at 08:32
  • I want to call other function. I already write that alert is just an example. – Georgi Naumov Jun 16 '14 at 08:34
  • 1
    chaining expression with semicolon between them should work. but I advise against it. you should call a function in your controller that will call all the functions you want to trigger. – Yaron Schwimmer Jun 16 '14 at 08:36
  • Then something else is wrong in your code that you have not yet shown us. This kind of errors are easily spotted using the built in console and debugger in most modern browsers. – Sergiu Paraschiv Jun 16 '14 at 08:36

1 Answers1

4

Simply wrap them in a function for a best practice:

$scope.myFunc = function(){
    secondTerm.keyphrase.selected=!secondTerm.keyphrase.selected;
    $window.alert(1);
}

data-ng-click="myFunc()"
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • Hi Amir - in your `$scope.myFunc`, could we consider this a function expression and therefor put a semicolon after the closing `}`, so that it looks like `};`? I'm curious if this breaks the Angularjs app or makes no difference. – Kyle Vassella Jun 22 '18 at 16:15
  • @KyleVassella - It's not mandatory but it's recommended. Read here: https://stackoverflow.com/questions/1834642/why-should-i-use-a-semicolon-after-every-function-in-javascript – Amir Popovich Jun 22 '18 at 17:07