仅隔离单击的元素

时间:2014-01-19 08:55:30

标签: javascript angularjs angularjs-directive angularjs-scope

我刚开始使用Angular,并且在理解某些核心概念时遇到了一些障碍。为了更好地熟悉这个新框架,我试图构建一个简单的应用程序:“你愿意吗?”。我向用户提出两个问题,他们选择一个,我突出他们的选择,并显示每个问题与以前的用户有多少票。

这听起来很简单,但我仍然坚持jQuery框架;我想根据$(this)或$(“#id”)选择元素。

我有一个带有一系列问题对象的工厂。每个对象都有一个映射到问题的firstQuestionsecondQuestion键,以及一个带有相应投票数的firstVotessecondVotes键。我正在使用QuestionsCtrl来控制范围,并在用户做出选择时采取行动。

这是我的 index.html 文件:

<!DOCTYPE html>

<html>

<head lang="en">
  <meta charset="utf-8">
  <title>Would You Rather?</title>
  <link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
  <div class="container" ng-app="wouldYouRatherApp">
    <div ng-controller="QuestionsCtrl">
      <div class="container">
        <div class="row text-center">
          <h1 class="col-md-12">Would you rather...</h1>  
        </div>
        <div class="row text-center">
          <div class="col-md-12">
            <h2 class="btn btn-lg btn-{{buttonClass}}" ng-click="recordAnswer('first')">{{question.firstQuestion}}</h2>
            <span class="badge" ng-show="badge.show">{{question.firstVotes}}</span>
          </div>
        </div>
        <div class="row text-center">
          <h3 class="col-md-12"><small>or</small></h3>
        </div>
        <div class="row text-center">
          <div class="col-md-12">
            <h2 class="btn btn-lg btn-{{buttonClass}}" ng-click="recordAnswer('second')">{{question.secondQuestion}}</h2>
            <span class="badge" ng-show="badge.show">{{question.secondVotes}}</span>
          </div>
        </div>
        <br>
        <div class="row text-center">
          <div class="col-md-12">
            <h2 class="btn btn-lg btn-primary" ng-show="showNextQuestion" ng-click="nextQuestion()">Next Question&nbsp;&nbsp;<span class="glyphicon glyphicon-forward"></span></h2>
          </div>
        </div>
      </div>
    </div>  
  </div>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.3/angular.min.js"></script>
  <script type="text/javascript" src="./main.js"></script>
</body>
</html>

这是我的 main.js 文件:

var app = angular.module("wouldYouRatherApp", []);

app.factory("Badges", function() {
    return {show: false}
})

app.factory("Questions", function() {
    var Questions = [{
        firstQuestion: "Ride a horse?",
        firstVotes: 101,
        secondQuestion: "Ride a cowboy?",
        secondVotes: 212
    },
    {
        firstQuestion: "Kiss a frog?",
        firstVotes: 13,
        secondQuestion: "Lick a slug?",
        secondVotes: 23
    },
    {
        firstQuestion: "Play Monopoly?",
        firstVotes: 12,
        secondQuestion: "Play Risk?",
        secondVotes: 17
    }];
    return Questions;
})

app.controller("QuestionsCtrl", function($scope, Badges, Questions) {
    $scope.question = Questions.shift();
    $scope.buttonClass = 'default';
    $scope.showNextQuestion = false;
    $scope.badge = Badges;

    $scope.recordAnswer = function(choice) {
        console.log("User chose: " + choice);
        $scope.buttonClass = 'success';
        // increment votes badge
        $scope[choice+'Votes'] += 1;
        Badges.show = true;
        $scope.showNextQuestion = true;
    }

    $scope.nextQuestion = function() {
        $scope.question = Questions.shift();
        Badges.show = false;
        $scope.buttonClass = 'default';
        $scope.showNextQuestion = false;
    }
})

可在此处找到实时示例:http://jsfiddle.net/t99TL/2/

应用程序的预期行为如下:

  1. 向用户提出两个问题。
  2. 用户点击其中一个按钮。
  3. 突出显示了这个问题。并且投票徽章会增加。两张投票徽章都会显示出来。
  4. 向用户显示“下一个问题”按钮。
  5. 当他/她点击“下一个问题”按钮时,会加载一个新问题。
  6. 我觉得我可能需要为每个问题创建一个指令......但是我不确定如何开始,或者我是否在正确的道路上。关于我将要进一步面对的障碍的任何建议都非常感激(即更新问题的投票属性等)。

1 个答案:

答案 0 :(得分:1)

要想让它以您想要的方式运作,还有很多需要改变的地方。这段代码并不完美,只是一个演示。

HTML:

<div class="container" ng-app="wouldYouRatherApp">

  <div ng-controller="QuestionsCtrl">
    <div class="container">
      <div class="row text-center">
        <h1 class="col-md-12">Would you rather...</h1>  
      </div>
      <div class="row text-center">
        <div class="col-md-12">
          <h2 class="btn btn-lg btn-{{question.firstQuestion.buttonClass}}" ng-click="recordAnswer(question.firstQuestion)">{{question.firstQuestion.text}}</h2>
          <span class="badge" ng-show="badge.show">{{question.firstQuestion.votes}}</span>
        </div>
      </div>
      <div class="row text-center">
        <h3 class="col-md-12"><small>or</small></h3>
      </div>
      <div class="row text-center">
        <div class="col-md-12">
          <h2 class="btn btn-lg btn-{{question.secondQuestion.buttonClass}}" ng-click="recordAnswer(question.secondQuestion)">{{question.secondQuestion.text}}</h2>
          <span class="badge" ng-show="badge.show">{{question.secondQuestion.votes}}</span>
        </div>
      </div>
      <br>
      <div class="row text-center">
        <div class="col-md-12">
          <h2 class="btn btn-lg btn-primary" ng-show="showNextQuestion" ng-click="nextQuestion()">Next Question&nbsp;&nbsp;<span class="glyphicon glyphicon-forward"></span></h2>
        </div>
      </div>
    </div>

  </div>  

JS:

var app = angular.module("wouldYouRatherApp", []);


app.factory("Badges", function() {
    return {show: false}
})


app.factory("Questions", function() {
    var Questions = [{
        firstQuestion: {
            text:"Ride a horse?",
            votes: 101,
            buttonClass : 'default'
        },
        secondQuestion: {
           text:"Ride a cowboy?",
           votes: 212,
            buttonClass : 'default'
        },
    },
    {
        firstQuestion: {
            text:"Kiss a frog?",
            votes: 13,
            buttonClass : 'default'
        },
        secondQuestion: {
            text:"Lick a slug?",
            votes: 23,
            buttonClass : 'default'
        }
    },
    {
        firstQuestion: {
            text:"Play Monopoly?",
            votes: 12,
            buttonClass : 'default'
        },
        secondQuestion: {
            text:"Play Risk?",
            votes: 17,
            buttonClass : 'default'
        }
    }];
    return Questions;
})


app.controller("QuestionsCtrl", function($scope, Badges, Questions) {
    $scope.question = Questions.shift();
    $scope.buttonClass = 'default';
    $scope.showNextQuestion = false;
    $scope.badge = Badges;

    $scope.recordAnswer = function(choice) {
        choice.buttonClass = 'success';
        choice.votes++;
        Badges.show = true;
        $scope.showNextQuestion = true;
    }

    $scope.nextQuestion = function() {
        $scope.question.firstQuestion.buttonClass = "default";
        $scope.question.secondQuestion.buttonClass = "default";

        $scope.question = Questions.shift();
        Badges.show = false;
        $scope.showNextQuestion = false;
    }
})

DEMO