简体   繁体   English

如何在Angular中切换视图?

[英]How do I switch views in Angular?

I am new to Angular, and have read all the tutorials, but am just starting out building my own app, so I am at the steep bit of the learning curve! 我是Angular的新手,已经阅读了所有教程,但是刚开始构建自己的应用程序,因此我处于学习曲线的绝大部分!

I am building a questionnaire. 我正在制作问卷。 I would like to show one question at a time, along with occasional optional content on each screen (depending on the answers given to the questions). 我想一次显示一个问题,并在每个屏幕上偶尔显示可选内容(取决于问题的答案)。

My question is about the cleanest way to structure this in my controller. 我的问题是关于在控制器中构造此结构的最简洁方法。

Currently my HTML looks like this: 目前,我的HTML如下所示:

<div ng-show="showIntro"> <!-- Intro, shown by default -->
  Intro
  <button ng-click="nextIntro">Next</button>
</div>

<div ng-show="showQ1"> <!-- Question 1, shown after the user clicks Next -->
  Question 1

<label class="checkbox-inline"> <!-- Radio buttons for user response -->
  <input type="radio" name="ast-adh-p1-q1" ng-model="q1aVal" 
   ng-change='answerQ1(q1aVal)' value="yes"> Yes
</label>
<label class="checkbox-inline">
  <input type="radio" name="ast-adh-p1-q1" ng-model="value" 
  ng-change='answerQ1(value)' value="no"> No
</label>

 <div ng-show="showQ1extra"> <!-- Shown if user answers yes to question 1 -->
   some extra content if the user answers yes to question 1 here
 </div>

 <button ng-click="nextQ1">Next</button>

</div>

<div ng-show="showQ2"> <!-- Question 2, shown after completing question 1 -->
  Question 2 ...
</div>

And my controller looks like this: 我的控制器如下所示:

    $scope.showIntro = true;
    $scope.showQ1 = false;
    $scope.showQ1extra = false;
    $scope.showQ2 = false;

    $scope.nextIntro = function() {
      $scope.showIntro = false;
      $scope.showQ1 = true;
    }

    $scope.answerQ1 = function(q1aVal) {
      $scope.showQ1extra = (q1aVal === 'yes') ? true : false;
    }

    $scope.nextQ1 = function() {
      $scope.showQ1 = false;
      $scope.showQ1extra = false;
      $scope.showQ2 = true;
    }

It works, but is inelegant and not scalable. 它可以工作,但是不优雅且不可扩展。 Is there a more sensible, Angular way to do it? 有更明智的Angular方法吗?

My own feeling is that there should be a $scope.activeSection parameter, that is a number, and that is initially set to 0. Then showIntro should return $scope.activeSection === 0 , etc, and there should be a single Next button that increments activeSection by 1 each time. 我自己的感觉是应该有一个$scope.activeSection参数,它是一个数字,最初设置为0。然后showIntro应该返回$scope.activeSection === 0$scope.activeSection === 0 ,并且应该有一个Next每次将activeSection递增1的按钮。 Does that sound like an Angular-friendly way of doing things? 这听起来像是一种Angular友好的工作方式吗?

Update: Here is a plunker with sample code: http://plnkr.co/edit/Ursyhc7YJYbJS5OCGYEr?p=preview 更新:这是一个带有示例代码的插件: http ://plnkr.co/edit/Ursyhc7YJYbJS5OCGYEr?p=preview

You can resolve this with a quite small amount of code using the "ngSwitch" directive . 您可以使用“ ngSwitch”指令以少量代码解决此问题。

HTML: HTML:

<div ng-switch="step">
    <div ng-switch-when="1">
        <p>This is step 1</p>
        <button ng-click="setStep(2)" class="btn btn-success">Go to step 2</button>
    </div>
    <div ng-switch-when="2">
        <p>This is step 2</p>
        <button ng-click="setStep(3)" class="btn btn-success">Go to step 3</button>
    </div>
    <div ng-switch-when="3">
        <p>This is step 3</p>
        <button ng-click="setStep(1)" class="btn btn-success">Back to start</button>
    </div>
</div>

And in you controller : 在您的控制器中:

$scope.step = 1;
$scope.setStep = function (num) {
    $scope.step = num;
};

You can check the result here : http://jsfiddle.net/Gg92r/ 您可以在这里检查结果: http : //jsfiddle.net/Gg92r/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM