You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AngularJS新手求助:自定义下拉列表指令(静态数据)无法工作

Fixing Your AngularJS Custom Dropdown Directive

Hey there! As an AngularJS newbie, it's totally normal to hit snags with custom directives and data binding. Let's break down why your static-data dropdown isn't working, and fix it step by step.

First, Let's Diagnose Common Issues

Your directive setup looks mostly right, but the problem is likely in one of these areas:

  • Your template file (device-branch-list.html) has incorrect data binding
  • You're not using the directive correctly in your main HTML
  • There's a tiny typo in your static data (I spotted valkue3 instead of value3—small mistakes like this can break bindings!)

Step-by-Step Fix

1. Correct Your Template File

First, let's make sure your device-branch-list.html uses proper AngularJS binding for the dropdown. Here are two reliable options:

Option 1: Using ng-repeat (simple approach)

<select ng-model="selectedBranch">
  <!-- Default "Select..." option -->
  <option value="" disabled selected>Select...</option>
  <!-- Bind to your static list -->
  <option 
    ng-repeat="branch in listBranch" 
    value="{{branch.deviceBranchId}}"
  >
    {{branch.deviceBranchName}}
  </option>
</select>

Option 2: Using ng-options (AngularJS-recommended for dropdowns)

This is cleaner and handles data binding more reliably:

<select 
  ng-model="selectedBranch"
  ng-options="branch.deviceBranchId as branch.deviceBranchName for branch in listBranch"
>
  <!-- Default option -->
  <option value="" disabled>Select...</option>
</select>

2. Fix the Typo in Your Static Data

In your directive's controller, correct the typo in the third list item to avoid display glitches:

app.directive("deviceBranch", function(){ 
  return { 
    templateUrl : 'resources/view/template/device-branch-list.html', 
    controller : function($scope) { 
      $scope.listBranch = [
        { deviceBranchId:"id1", deviceBranchName:"value1" },
        { deviceBranchId:"id2", deviceBranchName:"value2" },
        { deviceBranchId:"id3", deviceBranchName:"value3" } // Fixed typo here
      ] 
    } 
  } 
});

3. Use the Directive Correctly in Your Main HTML

AngularJS converts camelCase directive names to kebab-case when using them as elements. Make sure you're using:

<!-- Correct usage -->
<device-branch></device-branch>

<!-- WRONG: This won't work -->
<deviceBranch></deviceBranch>

4. Verify Basic Setup

Double-check these quick details too:

  • Your directive's JS file is properly included in your main HTML
  • The app module is correctly defined and attached to your HTML (e.g., <html ng-app="app">)

Test It!

Once you make these changes, your dropdown should load the static options, and the "Select..." default will work as expected. When you're ready to swap in dynamic data from a web service, just inject $http into your directive's controller and fetch the data there.

内容的提问来源于stack exchange,提问作者Amol Patil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:21:01