【问题标题】:Creating Directive for two views为两个视图创建指令
【发布时间】:2015-10-28 14:55:14
【问题描述】:

我正在尝试为两个视图创建指令。基本上我们有一个模式,带有页眉和页脚。当我们单击页脚中的继续按钮时,模态主体应该单独更改,将问题 2 显示为一个新视图,页眉和页脚已固定。我将如何使用指令来实现这一点? 我需要为每个视图创建单独的指令吗? 这是我的代码

modal.html -

<section>
    <header class="modal-header">
    </header>
    <div class="modal-body">
                question 1
    </div>
    <footer>
        <button>{{'BACK' | translate}}</button>
        <button type="submit" ng-click="showQuestion(2)">{{'CONTINUE' | translate}}</button>
    </footer>
</section>

directive.js

'use strict';
angular.module('OrderApp').directive('modalQuestions', [function () {
        function getQuestions() {
        return {
            restrict: 'A',
            templateUrl: 'modules/common/modals/modal.html',
            scope: true
        };
    }
]);

【问题讨论】:

    标签: javascript angularjs using-directives


    【解决方案1】:

    听起来您可能想使用ngSwitch 指令:

    <section>
        <header class="modal-header">
        </header>
        <div class="modal-body" ng-switch="questionNumber">
            <div ng-switch-when="1">
                    question 1
            </div>
            <div ng-switch-when="2">
                    question 2
            </div>
        </div>
        <footer>
            <button>{{'BACK' | translate}}</button>
            <button type="submit" ng-click="showQuestion(2)">{{'CONTINUE' | translate}}</button>
        </footer>
    </section>
    

    然后在你的控制器上:

    $scope.questionNumber = 1;
    $scope.showQuestion = function (questionNumber) {
        $scope.questionNumber = questionNumber;
    }
    

    $scope.questionNumber 更新时,ngSwitch 将更改它显示的 div。页面上的其他所有内容都将保持不变。

    【讨论】:

    • 我想知道这个 ng-click 功能是否应该分配在控制器范围或角度链接功能上。哪个更好?
    • 最简单的方法是在 imo 范围内分配它。根据您要显示的数据量以及数据更改的频率,您还可以选择 ng-if 而不是 ng-switch
    猜你喜欢
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 2012-09-03
    • 2013-08-21
    相关资源
    最近更新 更多