1

The following code is not hiding span, How can i hide the span.i am asking this question as i am new to angularjs.

<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
$scope.myVar=true;
</script>


<body ng-app="" >

<span ng-hide="myVar">
<a href="adminPage">Click here for admin role</a>
</span>

</body>

</html>
1
  • 1
    Go through the angularjs tutorials. You need to define a controller and then use that controller in your view. Commented Nov 1, 2016 at 12:21

2 Answers 2

1

You need a module first, then a controller or a directive.

<html ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
angular.module("myApp")
.controller("myController", function ($scope) {
$scope.myVar = true;
});
</script>


<body ng-controller="myController">

<span ng-hide="myVar">
<a href="adminPage" ng-click"myVar = !myVar">Click here for admin role</a>
</span>

</body>

</html>
Sign up to request clarification or add additional context in comments.

Comments

0

You haven't bootstrapped your application properly to make it an Angular app.

  1. You need to create a main module
  2. You need to attach the main module to the shell/index.html
  3. You need a Controller to perform data-binding, user interactions, etc

You need to write your very first "Hello, World!" app using AngularJS as it very common writing a "Hello, World!" application when you're getting started with any technology.

ng-app directive is used to define the part of the HTML which will be an Angular app, it's value is an optional application module name to load. See this post about using-ng-app-without-a-value

Show or hide content using Angular 1.0.1 without specifying the main module in the ng-app directive

//angular
  //.module('demo', [])
  //.controller('DefaultController', DefaultController);

//DefaultController.$inject = ['$scope'];

function DefaultController($scope) {
  $scope.isAdmin = true;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<div ng-app="">
  <div ng-controller="DefaultController">
    <span ng-show="isAdmin">
      <a href="#">Click here for admin role</a>
    </span>
    <span ng-hide="isAdmin">
      <a href="#">Click here for user role</a>
    </span>
  </div>
</div>

Please check the below example using $scope object to show or hide content by specifying the main module.

angular
  .module('demo', [])
  .controller('DefaultController', DefaultController);

DefaultController.$inject = ['$scope'];

function DefaultController($scope) {
  $scope.isAdmin = true;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demo">
  <div ng-controller="DefaultController">
    <span ng-show="isAdmin">
      <a href="#">Click here for admin role</a>
    </span>
    <span ng-hide="isAdmin">
      <a href="#">Click here for user role</a>
    </span>
  </div>
</div>

Please check the below example using controller aliasing syntax, to show or hide content by specifying the main module.

angular
  .module('demo', [])
  .controller('DefaultController', DefaultController);

function DefaultController() {
  var vm = this;
  vm.isAdmin = false;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demo">
  <div ng-controller="DefaultController as ctrl">
    <span ng-show="ctrl.isAdmin">
      <a href="#">Click here for admin role</a>
    </span>
    <span ng-hide="ctrl.isAdmin">
      <a href="#">Click here for user role</a>
    </span>
  </div>
</div>

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.