最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Open a new window in AngularJS - Stack Overflow

programmeradmin1浏览0评论

I know this is simple and a dumb question but I can't for the life of me make this work for some reason...

So I have a link (based on a non-angular popup using onclick):

<a href="page.html" ng-click="popitup('page.html')">Page</a>

And a function within the scope:

$scope.popitup = function(url) {
  return $window.open(url, '_blank', 'height=200,width=150');
};

All I want is, when someone clicks the link, open a new window and display page.html. I have tried changing small parts of the link and the function, but I can't get it. I'm sure it's something small I am doing wrong.

I know this is simple and a dumb question but I can't for the life of me make this work for some reason...

So I have a link (based on a non-angular popup using onclick):

<a href="page.html" ng-click="popitup('page.html')">Page</a>

And a function within the scope:

$scope.popitup = function(url) {
  return $window.open(url, '_blank', 'height=200,width=150');
};

All I want is, when someone clicks the link, open a new window and display page.html. I have tried changing small parts of the link and the function, but I can't get it. I'm sure it's something small I am doing wrong.

Share Improve this question asked Oct 19, 2015 at 19:54 elykl33telykl33t 9976 gold badges11 silver badges25 bronze badges 5
  • Are you passing in $scope and $window in your controller function like in the docs example? docs.angularjs/api/ng/service/$window – Jbird Commented Oct 19, 2015 at 19:58
  • AH! That might be it.. brb piling.. – elykl33t Commented Oct 19, 2015 at 20:01
  • Nevermind. I added $window and $scope was already there. Didn't seem to change anything. – elykl33t Commented Oct 19, 2015 at 20:08
  • Try assigning a reference to the method's return value. It may help you debug. var newWin = $window.open(url, '_blank', 'height=200,width=150'); Then log/dir the newWin var – Jbird Commented Oct 19, 2015 at 20:11
  • Oh and try using a fully qualified url for page.html – Jbird Commented Oct 19, 2015 at 20:13
Add a ment  | 

2 Answers 2

Reset to default 2

Try this.. In HTML

  <body ng-controller="MainCtrl">
  <a  role="link" ng-click="popitup('page.html', '_blank')">Page</a>
  </body>

In controller

app.controller('MainCtrl', function($scope, $window) {
  $scope.popitup = function(url, target) {
  $window.open(url, target);
};
});

plunker

This worked for me.

html

 <a data-ng-click="openPrintMode()">
                            <i class="fa fa-print"></i>
                        </a>

controller

$rootScope.openPrintMode = openPrintMode;

function openPrintMode() {
            $window.open($location.$$absUrl + "?v=p", "_blank");
        }
发布评论

评论列表(0)

  1. 暂无评论