0

This is the first time to use angular js.

The following code gives desired output:

<a href="http://www.example.com/xyz/{{life.animal}}/" target="_blank">Hello </a>

It opens a page: http://www.example.com/xyz/cat/

But the below code is not working as expected:

<a href='javascript:void(0)' onClick='javascript:window.open("http://www.example.com/xyz/{{life.animal}}/","Windows","width=1150,height=450,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,titlebar=no,location=no,directories=no,status=no");return false')'>Hello </a>

It opens page: http://www.example.com/xyz/{{life.animal}}/

I think I am doing some basic mistake but please help me.

1
  • I used target="_blank" instead. Thanks all for the help. Commented Oct 8, 2016 at 21:22

3 Answers 3

2

The Angular scope is not available outside of Angular, i.e. vanilla JS.

To set a click function the angular way you should use ng-click

Create a function in your controller or directive such as

scope.open = function() { $window.open(...) }

In the template do

ng-click="open()"

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

Comments

1

angular doesn't interact with strings, but you try to do that in your onclick handler that opens a window (you pass a string there). Stop the string and concat with the variable:

onClick='javascript:window.open("http://www.example.com/xyz/" + life.animal)'

Also, as @Enzey has noted, use ng-click instead of onClick, and then bring out the javascript from your html and do that stuff in a controller instead.

E.g.:

HTML

<a href="javascript:void(0)" ng-click="myFunction()">Foo</a>

Controller

$scope.myFunction = function() {
    window.open(...whatever...);
}

1 Comment

Also, one should use an onclick event on the element, to open it on a popup window. Also, even better, is to use target="_blank" instead.
1

The double hash are used for angular directives not for Javascript Vanilla apart of that is better if you use ng-href instead href in a <a> tag. You can check this here

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.