The Angular UI Bootstrap Accordion currently has the first accordion group closed instead of opened

I am currently utilizing UI-Bootstrap Accordion to populate content from a JSON Object. My goal is to have the first accordion group open by default upon loading, but unfortunately, this feature is not working as expected.

Although I have set is-open to true, it still doesn't seem to be functioning properly. For reference, please take a look at the following Plunker:

Plunker

<uib-accordion close-others="oneAtATime">
        <uib-accordion-group ng-repeat="faq in FAQs" is-open="this.open">
            <uib-accordion-heading>
                <div>{{faq.title}}<i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-up': this.open, 'glyphicon-chevron-down': !this.open}"></i>
                </div>
            </uib-accordion-heading>
            <div  ng-bind-html="faq.content"></div>
        </uib-accordion-group>
        </uib-accordion>

Thank you for your assistance.

Answer №1

Angular comes with some built-in variables for ng-repeat like $index (0..length-1),

$first, $middle, $last, $even, $odd
. These variables can make your work easier :-)

For more information, you can refer to: https://docs.angularjs.org/api/ng/directive/ngRepeat

If you want your expected output, simply use $first in your is.open

<uib-accordion close-others="oneAtATime" ng-repeat="faq in FAQs">
            <uib-accordion-group is-open="$first">
                <uib-accordion-heading>
                    <div>{{faq.title}}<i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-up': this.open, 'glyphicon-chevron-down': !this.open}"></i>
                    </div>
                </uib-accordion-heading>

                <div  ng-bind-html="faq.content"></div>
            </uib-accordion-group>
             <uib-accordion-group ng-if="!$first" is-open="false">
                <uib-accordion-heading>
                    <div>{{faq.title}}<i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-up': this.open, 'glyphicon-chevron-down': !this.open}"></i>
                    </div>
                </uib-accordion-heading>
 
                <div  ng-bind-html="faq.content"></div>
            </uib-accordion-group>
            </uib-accordion>

I have also included the Plunker link that I previously shared in my comment :-

plnkr.co/edit/iWEuuv?p=preview

Answer №2

Simply update is-open="this.open" to is-open="$first" and it will function correctly.

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap','ngSanitize']);
angular.module('ui.bootstrap.demo').controller('AccordionDemoCtrl', function ($scope) {
  $scope.oneAtATime = true;

  $scope.FAQs = [
    {
      title: 'Overview',
      content: '<p>This Add-in reviews <b>External Users</b> for a Site Collection if number of days remaining to expire is <b>10 days</b>.Then generate and send <b>warning email</b> notifications to Site Collection Administrators/Person who sent external User Invite and Governance Team Memberrs.The warning email notification will contain following options :<ol><li> Extend Access</li><li> Remove User </li></ol></p><p>The same information regarding external users can be viewed through Site Actions Menu, Site Settings page and one time warning pop-up on site collection. This add-in also provisions an option in Site Actions Menu to display <b>Site Collection administrators</b>.</p>'
    },
    {
      title: 'Who can use?',
      content: '<p>Users with only <b>admin</b> priviledges can view the external user details and edit their access time . However, <b>all</b> users can view the site administrator details. </p>'
    },
    {
      title: 'How to Use?',
      content: '<p><ul><li>The add-in will be installed through <b>Spo.Provision</b> utility.</li><li>Right click on a row to perform \'Extend Access\' or \'Remove User\' operations from context menu.</li><li>In order to perform these opeartions for multiple users, select rows by holding <b>shift/Ctrl</b> keys and click on <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> for extending access and <span class="glyphicon glyphicon-minus" aria-hidden="true"></span> for removing selected users. </li><li><b>  Please note</b> that you cannot extend access if user already has access for maximum limit.</li></ul></p>'
    },
    {
      title: 'Settings & Configuration',
      content: '<p><ul><li>The configuration for timer job such as cut-off time and warning message can be done using App.Config file.</li></ul></p>'
    },
    {
      title: 'Compatibility',
      content: '<p>Compatible with IE10 and upper version. Latest version of Chrome & Firefox.</p>'
    }
  ];

  $scope.status = {
    isFirstOpen: true
  };
});
<!doctype html>
<html ng-app="ui.bootstrap.demo">
<head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.5.0/angular-sanitize.min.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.3.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div ng-controller="AccordionDemoCtrl">
<h1>OBJECT</h1>
<uib-accordion close-others="oneAtATime">
            <uib-accordion-group ng-repeat="faq in FAQs" is-open="$first">
                <uib-accordion-heading>
<div>{{faq.title}}<i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-up': this.open, 'glyphicon-chevron-down': !this.open}"></i>
</div>
</uib-accordion-heading>
<div  ng-bind-html="faq.content"></div>
            </uib-accordion-group>
</uib-accordion>
    </div>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attempted to create a node index.js file but encountered an error

Whenever I try to run express node.js on "localhost:3000", I keep getting an error message in my hyper terminal that says "unexpected token =". Here is the code snippet: //JS hint esversion:6 const = require("express"); const app = express(); app.get("/ ...

Execute JavaScript using Ajax technology

Although it has been discussed previously, my knowledge of javascript is quite limited, so I find myself a complete beginner. I am currently utilizing a javascript code that sends variables to a php file, and the information is then loaded into the current ...

JavaScript code must be tailored to reference a JS file based on the specific environment, whether it be Production, Development, or staging

I need to determine which .js file to refer based on the URL of Prod, Dev, and QA environments. For Production URLs (domain.com and www.domain.com), I should refer to prod.js file. For Dev and QA URLs (dev.domain.com and staging.com), I should refer to s ...

Copying elements from ng-repeat scope to outside

I am brand new to AngularJS and I've been trying to figure out a solution for quite some time now without any luck. Basically, I have a form where users can add new fields. These fields are then displayed in a list at the bottom using ng-repeat. When ...

Give a discount to each object in an array based on whichever object has the highest paid value in the loop

In my array of objects, each object has a key paid. I am looking to apply a discount to all objects except for the one with the highest paid value using a loop. var booking = [ {id:1,paid:200,currency:'USD'}, {id:2,paid:99,currency:'USD&apos ...

When the tooltip component is triggered in a React application, the cursor is automatically directed to

I have been working on creating an input field that allows users to enter time as input. Here's the code snippet I am using: <InputMask mask="99:99" onBlur={handleOnBlur} onChange={(e) => { const ...

An elementary React project facing compilation issues

I'm currently exploring react hooks, but I encountered an error with the useReducer hook. The console displays the following error message: "Invalid hook call. Hooks can only be called inside of the body of a function component. This could happe ...

Acquire the nested object within a MongoDB document using its assigned ID

Looking to retrieve and edit a specific comment within a post, but not sure where to start. Here is an example of my Post data structure: { "title" : "First Node.js App", "body" : "testing 123", "st ...

Deliver integers using Express.js

When I try to send a response with Express using the code below, it doesn't work: res.send(13245) An error message is displayed: express deprecated res.send(status): Use res.sendStatus(status) instead src/x.js:38:9 (node:25549) UnhandledPromise ...

Using an AngularJs directive to perform form validation on an input field

Currently, I am working on a project where there are multiple submit forms with similar fields. To streamline the process, I have made the decision to transfer some fields to directives. Here is an example of the form: <div loading class="col-sm-1 ...

In TypeScript, the Select element does not contain an options property

Having trouble iterating through a TypeScript array. Here are the methods I'm using: getNotification(evt: string, rowIndex: number) { console.log("Production order: law has changed to " + evt + " " + rowIndex); var select = document.getEleme ...

Attempting to loop through a list and create a retrieval function for each item

My goal is to loop through the style tags and create a GET function for each one. The issue I'm facing is that the GET function is being written with a direct reference to 'styleTags[i]' instead of converting it to the appropriate tag. var ...

The content entered into the text area does not appear as it was originally typed

Users have reported an issue with the input area - when they update the text, it does not display as expected. For instance, new lines are not being shown. Here is a snapshot of what the user typed in: https://i.sstatic.net/JK0OQ.png However, after upda ...

What is the best way to make an HTML form show fields depending on certain conditions?

Initially, I created an index page containing a form with various fields. The utility was built to handle all the fields, but now there's been a change in requirements. What I need is for only the Controller Type and Test Type fields to be displayed f ...

Issue with vue-router redirection when pushing a route

I have set up my router configuration as follows: const router = new Router({ mode: 'history', routes: [ // root { path: '/', component: ComponentPage, redirect: routePrefix.public, children: [ ...

Exploring the world of AngularJS 1.3 with the exciting anticipation of the upcoming release of Angular 2 in just one year

Currently contemplating learning AngularJS 1.3, but concerned about the upcoming release of version 2 and the significant changes that will come with it. Is it worth investing time and effort into mastering a framework that is soon to be obsolete? Seekin ...

Maintain checkbox selection even after the page is refreshed

Having trouble fetching all objects from the favorites array and setting the checkbox to checked. I've attempted using localStorage but the values are not saved after refreshing, despite researching online for solutions. Any assistance would be great ...

HTML code that has been "commented out" means

Within my _Layout.cshtml file, the following lines are present: <!--[if IE 7]> <link rel="stylesheet" type="text/css" media="all" href="/Content/css/ie7.css" /> <![endif]--> <!--[if IE 6]> <link rel="stylesheet" type="te ...

Working efficiently with query selectors in React using useRef

I have created a typewriting effect function and now I am trying to display the code associated with this effect within a specific div element (typingRef). Currently, I am using typingRef.current = letter, but I am wondering if this is equivalent to docu ...

sending properties to child components without all the required properties

Looking for a way to pass props down to children components? const Tabs = ({ children, props }) => { console.log(props) //why is activeTab undefined?? return React.Children.map(children, child => React.cloneElement(child, { ...props }) ) } ...