1

I have 2 div elements as following and I want to show only one of them based on the doStuff() function being called in the controller when an anchor element is clicked.

<div ng-controller='myController'>
    <div ng-show="{{states['currentState'] == 'A'}}">
        //displaying this div if the currentState is A
        <a ng-click="doStuff('B')">Do stuff and show B</a>
    </div>
    <div ng-show="{{states['currentState'] == 'B'}}">
        //displaying this div if the currentState is B
    </div>
</div>

Following is the controller code:

myApp.controller('myController', ['$scope', function($scope) {

  var states = ['A', 'B'];
  $scope.states = states;
  $scope.states['currentState'] = $scope.states['currentState'] || 'A';

  $scope.doStuff = function(stateToShow) {
    //doing stuff
    $scope.states['currentState'] = stateToShow;
  };

}]);

The code above doesn't work as the state remains 'A' even after clicking the Do stuff and show B anchor element.

Could somebody help me understand why is it not working?

Edit

app.js

 //...

    .state('home', {
        url: '/',
        views: {

            '': { templateUrl: 'partials/index.html' },

            'myView@home': {
                templateUrl: 'partials/myView.html',
                controller: 'VehicleController'
            }
            //other named ui views
        }

    })

 //...  

index.html

<div class="main">
    <div class="container">
        <div class="row margin-bottom-40">
            <div class="col-md-12 col-sm-12">
                <div class="content-page">
                    <div class="row">
                        <div ui-view="myView"></div>
                        <!-- other named ui-views -->
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

myView.html

<div ng-controller='myController'>
    <div ng-show="states['currentState'] == 'A'">
        //displaying this div if the currentState is A
        <a ng-click="doStuff('B')">Do stuff and show B</a>
    </div>
    <div ng-show="states['currentState'] == 'B'">
        //displaying this div if the currentState is B
    </div>
</div>

2 Answers 2

4

It is updating the scope. But possibly the issue is with the ng-show you are setting a string by using "{{notation}}" which becomes truthy always (even if it is "true" or "false"), just use the expression directly.

Change

 <div ng-show="{{states['currentState'] == 'A'}}">

to

 <div ng-show="states.currentState === 'A'">

Demo

From Doc:-

ngShow expression - If the expression is truthy then the element is shown or hidden respectively.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you PLS and Unome, I corrected that but I am still not being able to get the updated value of {{states.currentState}} in the html just like its happening in the JS Bin demo you showed. Is it possible that its not working because my html code is being used as the named ui view in the Angular UI Router module? It keeps showing the same value of {{states.currentState}} even after the click the anchor tag.
@skip Not sure, looking at the details that you have in your question, Can you share your actual working code in a jsbin/plnkr? if you are using named views (using ui router) do you really need to do it this way.. You could just switch the views on click right?
I've just edited the question by adding some more code for the given issue I am facing. Could you please have a look at it?
@PLS: I basically already have a nested view on index.html page, but I have more than one named views on the same index.html page as well. I am basically trying to give my page a portlet like effect, so I needed both the nested view and named views. I am quite new to AngularJS, I hope I am not committing a blunder here doing things like this.
@skip Seems fine to me... You dont have to (should not) use scope.$apply when already inside angular. plnkr.co/edit/aNjz3mO29nkfllbJpqN6?p=preview
|
1

You are very close. The reason it is not working is the attribute "ng-show" does not need the "{{" "}}" notation to work.

I just built your code but took those off, and it is working as you described you wanted it to.

<div ng-show="states['currentState'] == 'A'">
    //displaying this div if the currentState is A
    <a ng-click="doStuff('B')">Do stuff and show B</a>
</div>
<div ng-show="states['currentState'] == 'B'">
    //displaying this div if the currentState is B
</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.