Skip to content

2015

Angular UI Bootstrap: Opening the First Accordion with ng-repeat

The Problem:

I was using the accordion directive in Angular UI Bootstrap version 0.1.2. On the demo page, there's an example showing how to open the first accordion group by default:

<accordion-group heading="First Header" is-open="true"> </accordion-group>

This works well for static content but fails to operate as expected with dynamic content generated using ng-repeat. In other words, it does not work like this:

<accordion-group
  heading="{{group.title}}"
  ng-repeat="group in groups"
  is-open="true"
>
</accordion-group>

My Solution:

In the template, bind the is-open property of the accordion as follows:

<accordion-group
  heading="{{group.title}}"
  ng-repeat="group in groups"
  is-open="status.isItemOpen[$index]"
>
</accordion-group>

And in the controller:

$scope.groups = ["First Header", "Second Header", "Third Header"]
$scope.status = {
  isItemOpen: new Array($scope.groups.length),
  isFirstDisabled: false,
}
$scope.status.isItemOpen[0] = true

Change the value of the last line to false if you prefer the first accordion to be closed by default.

Let me know if this doesn't work for you. 😊

Angular UI Bootstrap:使用ng-repeat開啟第一個手風琴

問題:

我在使用Angular UI Bootstrap版本0.1.2的手風琴指令。在示例頁面上,有一個範例顯示如何默認開啟第一個手風琴組:

<accordion-group heading="First Header" is-open="true"> </accordion-group>

這對於靜態內容來說工作得很好,但是對於使用ng-repeat生成的動態內容來說,它無法如預期地操作。換句話說,它不能這樣工作:

<accordion-group
  heading="{{group.title}}"
  ng-repeat="group in groups"
  is-open="true"
>
</accordion-group>

我的解決方案:

在模板中,將手風琴的is-open屬性綁定如下:

<accordion-group
  heading="{{group.title}}"
  ng-repeat="group in groups"
  is-open="status.isItemOpen[$index]"
>
</accordion-group>

並在控制器中:

$scope.groups = ["First Header", "Second Header", "Third Header"]
$scope.status = {
  isItemOpen: new Array($scope.groups.length),
  isFirstDisabled: false,
}
$scope.status.isItemOpen[0] = true

如果你希望第一個手風琴默認為關閉,則將最後一行的值改為false

如果這個解決方案對你沒有作用,請讓我知道。😊

Note on Ionic Framework: Android Platform in OS X

In this blog post, we will walk through several error messages that you might encounter when installing dependencies for the Android platform of the Ionic framework on Mac OS X. Follow this official guide to install Cordova and Ionic, and then create a project up until the step where you configure the platform.

To enable the Android platform, run the following command:

ionic platform add android

However, you may encounter this error:

Error: ANDROID_HOME is not set and "android" command not in your PATH.

Step 1: Download the Android SDK

Visit the Android developer website and download the stand-alone SDK Tools: https://developer.android.com/sdk/installing/index.html.

Step 2: Unzip the File

I unzipped the SDK to the path of my workspace:

/Users/Victor/Workspace/android-sdk-macosx

Step 3: Set the Android Command in Your PATH

Note: Replace "Victor" with your username. If you are using oh-my-zsh, replace the .bash_profile with your .zshrc profile.

  1. Open your terminal and ensure that .bash_profile ends with a newline:
echo >> /Users/Victor/.bash_profile
  1. Set the ANDROID_HOME environment variable in .bash_profile:
echo "export ANDROID_HOME=/Users/Victor/Workspace/android-sdk-macosx" >> /Users/Victor/.bash_profile
  1. Update the PATH variable as well:
echo "export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools" >> /Users/Victor/.bash_profile
  1. Reload the terminal to apply the changes:
. /Users/Victor/.bash_profile

Step 4: Install Android-19

Now, if you try to run:

ionic platform add android

You might see the following error:

Error: Please install Android target "android-19".

Run android from your command line to open the SDK manager:

android

Check the box for Android 4.4.2 (API 19) and then click "Install 8 packages…". Accept the license and keep your fingers crossed while waiting for the download process to complete.

Finally, try running the command again:

ionic platform add android

And voila:

Project successfully created.

If you encounter any issues, feel free to send me a help request. Cheers! ☺

關於Ionic框架的註解:OS X中的Android平台

在這篇博客文章中,我們將一起研究你在Mac OS X上為Ionic框架的Android平台安裝依賴時可能遇到的幾種錯誤信息。請按照這個官方指南來安裝Cordova和Ionic,然後創建一個項目,直到你配置平台的步驟。

要啟用Android平台,請運行以下命令:

ionic platform add android

然而,你可能遇到這個錯誤:

Error: ANDROID_HOME is not set and "android" command not in your PATH.

步驟1:下載Android SDK

訪問 Android 開發者網站並下載獨立的 SDK 工具:https://developer.android.com/sdk/installing/index.html

步驟2:解壓縮文件

我將SDK解壓縮到我的工作空間的路徑:

/Users/Victor/Workspace/android-sdk-macosx

步驟3:在您的PATH中設置Android命令

注意:將"Victor"替換成您的用戶名。如果您使用的是oh-my-zsh,請將.bash_profile替換成您的.zshrc配置文件。

  1. 打開您的終端並確保 .bash_profile 以一個新行結束:
echo >> /Users/Victor/.bash_profile
  1. .bash_profile中設置 ANDROID_HOME 環境變量:
echo "export ANDROID_HOME=/Users/Victor/Workspace/android-sdk-macosx" >> /Users/Victor/.bash_profile
  1. 也更新PATH變量:
echo "export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools" >> /Users/Victor/.bash_profile
  1. 重載終端以應用更改:
. /Users/Victor/.bash_profile

步驟4:安裝Android-19

現在,如果您嘗試運行:

ionic platform add android

您可能會看到以下的錯誤:

Error: Please install Android target "android-19".

從命令行運行 android 打開SDK管理器:

android

勾選 Android 4.4.2 (API 19),然後點擊 "Install 8 packages…”。接受授權並等待下載過程完成。

最後,再次嘗試運行命令:

ionic platform add android

然後就成功了:

Project successfully created.

如果您遇到任何問題,請隨時向我發送幫助請求。幹杯!☺