You can assign your state to a variable with a scope inside the controller:
angular.module('myApp', []).controller('MyCtrl', ['$scope', function( $scope ) { $scope.isChrome = /chrome/i.test(navigator.userAgent); });
Then in your view:
<div data-ng-show="isChrome">You're running Chrome!</div>
If you need a variable accessible to any controller, use the Service:
angular.module('myApp', []).factory('UAService', function() { return { isChrome: /chrome/i.test(navigator.userAgent) }; });
Then from the page level controller (i.e. assigned to a top level element such as <body>
) or any other controller, enter your service:
angular.module('myApp', []).controller('MyCtrl', ['$scope', 'UAService', function( $scope, UAService ) { $scope.UAService = UAService; });
Your service will now be accessible from any scope created by your page level controller:
<div data-ng-show="UAService.isChrome">You're running Chrome!</div>
source share