Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs fallback, hide for none javascript users

I know about the script-tag for adding content that none javascript users can read.

What is the best way to hide html blocks that is created for angularjs?

Example:

<div>
{{this_code_is_converted_to_angular_js_code_when_javascript_is_active}}
</div>

The line above is not that beautiful for a none javascript user. What is the best way to remove it if the user is not using javascript?

like image 776
Jens Törnell Avatar asked Mar 21 '26 09:03

Jens Törnell


2 Answers

Use the <noscript> tag to display content when JavaScript is not enabled.

Also the main.html file should not have any {{content}} tags, instead you should have sub views which are loaded into the <div ng-view=""></div>

This way if JavaScript is not enabled, only content in the noscript tag will be displayed because the ng-view div is empty, as AngularJS is JavaScript and will never insert anything into the ng-view div.

Example HTML

<!DOCTYPE html>
<!--[if lt IE 7]>      <html lang="en" ng-app="myApp" class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html lang="en" ng-app="myApp" class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html lang="en" ng-app="myApp" class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en" ng-app="myApp" class="no-js"> <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>My AngularJS App</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="bower_components/html5-boilerplate/css/normalize.css">
  <link rel="stylesheet" href="bower_components/html5-boilerplate/css/main.css">
  <link rel="stylesheet" href="css/app.css"/>
  <script src="bower_components/html5-boilerplate/js/vendor/modernizr-2.6.2.min.js"></script>
</head>
<body>
  <!--[if lt IE 7]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
  <![endif]-->

  <div ng-view></div>
  <noscript>Enable JavaScript to view this web page.</noscript>

  <!-- In production use:
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/x.x.x/angular.min.js"></script>
  -->
  <script src="bower_components/angular/angular.js"></script>
  <script src="bower_components/angular-route/angular-route.js"></script>
  <script src="js/app.js"></script>
  <script src="js/services.js"></script>
  <script src="js/controllers.js"></script>
  <script src="js/filters.js"></script>
  <script src="js/directives.js"></script>
</body>
</html>
like image 51
furier Avatar answered Mar 22 '26 23:03

furier


If you just want to hide elements you can use ng-cloak https://docs.angularjs.org/api/ng/directive/ngCloak

like image 22
maurycy Avatar answered Mar 22 '26 22:03

maurycy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!