手风琴

时间:2017-10-05 17:19:06

标签: javascript angularjs accordion

我希望在下面的场景中有一个动态ng-model名称。在选择部门框时,我将绘制表格。

适用于, ng-model="department.name",但由于我们还有一些其他内部因素,它会导致其他一些问题。我想将名称设为ng-model={{department.name}},以便名称本身是动态的。

<div ng-app >
    <ul ng-repeat="department in selectedDepartments">
        <li>
            <div>{{department.id}}</div>
            <input type="text" ng-model={{department.name}}" >
        </li>
    </ul>
</div>

function DepartmentController($scope) {

    $scope.selectedDepartments = {};

    $scope.departments = [
    "audit": [{id:0,name:"auditDept0"},{id:0,name:"auditDept1"}],
    "hr": [{id:0,name:"hrDept0"},{id:0,name:"hrDept1"}],
    "finance": [{id:0,name:"financeDept0"},{id:0,name:"financeDept1"}]
    ];

    $scope.selectDepartment = function(name) {
      if(name=="hr") {
        $scope.selectedDepartments =     $scope.departments.hr;
      } else if(name=="finance") {
        $scope.selectedDepartments =     $scope.departments.finance;
      }
    }
}​

我尝试创建如下指令。

this.app.directive('dynamicModel', ['$compile', '$parse', function ($compile, $parse) {
    return {
        restrict: 'A',
        terminal: true,
        priority: 100000,
        link: function (scope, elem) {
            var name = $parse(elem.attr('dynamic-model'))(scope);
            elem.removeAttr('dynamic-model');
            elem.attr('ng-model', name);
            $compile(elem)(scope);
        }
    };
}]);

但是,我仍然无法使用dynamic-model={{department.name}}

由于

1 个答案:

答案 0 :(得分:1)

我认为不需要自定义指令。您可以使用ng-models中的javascript表达式来使用动态模型。这是一个example

在你的情况下,我认为你正在寻找像这样的东西

/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/mutex/mut_tas.c:162:34: error: unrecognized instruction mnemonic
                if (MUTEXP_IS_BUSY(mutexp) || !MUTEXP_ACQUIRE(mutexp)) {
                                               ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/dbinc/mutex_int.h:1099:2: note: expanded from macro 'MUTEXP_ACQUIRE'
        MUTEX_SET(&(mutexp)->tas))
        ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/dbinc/mutex_int.h:477:3: note: expanded from macro 'MUTEX_SET'
                "ldrex          %0, [%2]\n\t"                           \
                ^
<inline asm>:1:2: note: instantiated into assembly here
        ldrex           x8, [x28]
        ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/mutex/mut_tas.c:162:34: error: unrecognized instruction mnemonic
                if (MUTEXP_IS_BUSY(mutexp) || !MUTEXP_ACQUIRE(mutexp)) {
                                               ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/dbinc/mutex_int.h:1099:2: note: expanded from macro 'MUTEXP_ACQUIRE'
        MUTEX_SET(&(mutexp)->tas))
        ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/dbinc/mutex_int.h:478:17: note: expanded from macro 'MUTEX_SET'
                "cmp            %0, %1\n\t"                             \
                                        ^
<inline asm>:3:2: note: instantiated into assembly here
        strexne x8, x20, [x28]
        ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/mutex/mut_tas.c:162:34: error: unrecognized instruction mnemonic
                if (MUTEXP_IS_BUSY(mutexp) || !MUTEXP_ACQUIRE(mutexp)) {
                                               ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/dbinc/mutex_int.h:1099:2: note: expanded from macro 'MUTEXP_ACQUIRE'
        MUTEX_SET(&(mutexp)->tas))
        ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/dbinc/mutex_int.h:477:3: note: expanded from macro 'MUTEX_SET'
                "ldrex          %0, [%2]\n\t"                           \
                ^
<inline asm>:1:2: note: instantiated into assembly here
        ldrex           x8, [x28]
        ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/mutex/mut_tas.c:162:34: error: unrecognized instruction mnemonic
                if (MUTEXP_IS_BUSY(mutexp) || !MUTEXP_ACQUIRE(mutexp)) {
                                               ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/dbinc/mutex_int.h:1099:2: note: expanded from macro 'MUTEXP_ACQUIRE'
        MUTEX_SET(&(mutexp)->tas))
        ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/dbinc/mutex_int.h:478:17: note: expanded from macro 'MUTEX_SET'
                "cmp            %0, %1\n\t"                             \
                                        ^
<inline asm>:3:2: note: instantiated into assembly here
        strexne x8, x22, [x28]
        ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/mutex/mut_tas.c:213:3: error: too few operands for instruction
                MEMBAR_ENTER();
                ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/dbinc/mutex_int.h:492:23: note: expanded from macro 'MEMBAR_ENTER'
        ({ __asm__ volatile ("dsb"); })
                             ^
<inline asm>:1:2: note: instantiated into assembly here
        dsb
        ^~~
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/mutex/mut_tas.c:439:3: error: too few operands for instruction
                MEMBAR_ENTER();
                ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/dbinc/mutex_int.h:492:23: note: expanded from macro 'MEMBAR_ENTER'
        ({ __asm__ volatile ("dsb"); })
                             ^
<inline asm>:1:2: note: instantiated into assembly here
        dsb
        ^~~
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/mutex/mut_tas.c:623:4: error: too few operands for instruction
                        MEMBAR_EXIT();
                        ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/dbinc/mutex_int.h:494:23: note: expanded from macro 'MEMBAR_EXIT'
        ({ __asm__ volatile ("dsb"); })
                             ^
<inline asm>:1:2: note: instantiated into assembly here
        dsb
        ^~~
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/mutex/mut_tas.c:635:3: error: too few operands for instruction
                MEMBAR_EXIT();
                ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/dbinc/mutex_int.h:494:23: note: expanded from macro 'MEMBAR_EXIT'
        ({ __asm__ volatile ("dsb"); })
                             ^
<inline asm>:1:2: note: instantiated into assembly here
        dsb
        ^~~
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/mutex/mut_tas.c:619:4: error: too few operands for instruction
                        MEMBAR_EXIT();
                        ^
/home/mariano/Downloads/db-6.2.32/build_android/jdbc/jni/../../../src/dbinc/mutex_int.h:494:23: note: expanded from macro 'MEMBAR_EXIT'
        ({ __asm__ volatile ("dsb"); })
                             ^
<inline asm>:1:2: note: instantiated into assembly here
        dsb
        ^~~
7 warnings and 9 errors generated.
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {

    $scope.selectedDepartments = {};

    $scope.departments = {
        "audit": [{id: 0, name: "auditDept0"},
                  {id: 0, name: "auditDept1"}],
        "hr": [{id: 0, name: "hrDept0"}, 
               {id: 0, name: "hrDept1"}],
        "finance": [{ id: 0, name: "financeDept0"},
                    {id: 0, name: "financeDept1"}]
    };

    $scope.selectDepartment = function(name) {
        if (name == "hr") {
            $scope.selectedDepartments = $scope.departments.hr;
        } else if (name == "finance") {
            $scope.selectedDepartments = $scope.departments.finance;
        }
    }

    $scope.selectDepartment('hr');
});

我希望这会对你有所帮助