Samantha J Samantha J - 3 months ago 8
AngularJS Question

How can I set a variable in $rootscope to show if a browser is running on an iPad?

I know there are some libraries that can do this but I would like to do something as simple as possible and I didn't see any examples that were for an AngularJS app. The problem is I do not know where to start.

Can someone give me some advice or point me in the right direction.

Answer

angular
  .module('test', [])
  .run(function($rootScope) {
    $rootScope.isIpad = /ipad/i.test(navigator.userAgent);
  })
;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<section ng-app="test">
  <h1>is iPad? <span ng-bind="isIpad | json"></span></h1>
</section>


if you want to have a more robust solution, have a look here Detect iPad users using jQuery?

Comments