0

所以我试图完成这个例子:http: //jsfiddle.net/pkozlowski_opensource/WXJ3p/15/

但是,由于某种原因,当我单击一个 div,然后单击另一个 div 时,它不会从前一个 div 中删除“活动”类,因此它会突出显示两者,因此如果我单击全部,我的所有 div 都会以活动类结束其中。如果我单击主体上的其他任何位置以及单击任何其他 div (例如小提琴示例),我想将其移至实际删除该类的位置。

我的 jsFiddle http://jsfiddle.net/GeorgiAngelov/jUj56/4/

<div ng-controller="MyCtrl">
          <button class="addQuestionButton btn btn-primary" ng-click="AddRootQuestion(questions)">Add node</button>
        <div ng-repeat="question in questions" ng-include="question">     </div>

    <script type="text/ng-template" id="question">
    <!-- Single question starts here -->
        <div ng-controller="QuestionController" ng-class="{active : isSelected(question)}">
          <button class="btn btn-primary" ng-click="AddSubQuestion(question)">Add node</button>
          <button class="btn btn-primary" ng-click = "editQuestion(question)">Edit Question</button>
        </div>
        <div ng-repeat="question in question.nodes" ng-include="question">
        </div>                      
</script>
</div>
4

1 回答 1

3

由于每个问题都有自己的QuestionController,并且QuestionController是在哪里$scope.selected设置的,所以它们不会相互交互。也就是说,当你点击编辑时,它会selected 为那个单独的控制器设置。

修复它的简单方法是在单击编辑时在父范围(包含控制器)上设置属性:

function MyCtrl($scope) {
    $scope.questions = [];  

    $scope.AddRootQuestion = function(questions) {
        questions.push({name: 'Question', nodes: []});
    };

    // added this method --v
    $scope.setSelected = function(q) {
        $scope.selected = q;
    };
}

function QuestionController($scope) {
    $scope.choices = [];
    $scope.choice = {text: ''};

    $scope.AddSubQuestion = function(question, $element) {
      var newName = 'Name of question goes here';
      question.nodes.push({name: newName, id: 'it goes in here', nodes: []});
    };

    // modified this method --v
    $scope.editQuestion = function(question){
      $scope.setSelected(question);
    };

    $scope.isSelected = function(question) {
      return $scope.selected === question;
    };
}

但这使得QuestionController依赖于父控制器。一个更好的设计是将所有数据和数据操作方法移动到服务中:

var myApp = angular.module('myApp',[]);

myApp.factory('Question', function() {
  return {
    questions: [],

    addRootQuestion: function() {
      this.questions.push({name: 'Question', nodes: []});
    },

    addSubQuestion: function(question, data) {
      question.nodes.push(data);
    },

    setSelectedQuestion: function(question) {
      this.selectedQuestion = question;
    },

    isSelectedQuestion: function(question) {
      return this.selectedQuestion == question;
    }
  };
});

然后,您可以将服务注入您的控制器:

function MyCtrl($scope, Question) {
  $scope.questions = Question.questions;  

  $scope.AddRootQuestion = function() {
    Question.addRootQuestion();
  };
}

function QuestionController($scope, Question) {
  $scope.choices = [];
  $scope.choice = {text: ''};

  $scope.AddSubQuestion = function(question, $element) {
    var newName = 'Name of question goes here';
    Question.addSubQuestion(question, {
      name: newName, id: 'it goes in here', nodes: []
    });
  };

  $scope.editQuestion = function(question){
    Question.setSelectedQuestion(question);
  };

  $scope.isSelected = function(question) {
    return Question.isSelectedQuestion(question);
  };
}

示例:http: //jsfiddle.net/BinaryMuse/pZkBv/

如果您愿意,您可以使用 JavaScript OOP 原则构建更丰富的数据模型;例如,您可以拥有Question处理问题的方法的实例,这些问题本身存在于QuestionContainer(或SurveyTest或其他)的实例中。

于 2013-07-06T20:30:15.703 回答