angularjs - 如何将标签或属性关联到 angularjs 中的控件?

angularjs - How can I associate a label or attribute to a control in angularjs?

我有一个在 CMS 中使用的 angular 应用程序。营销团队可以通过使用普通 html 添加 html 到小部件来创建表单。我想要做的是捕获表单中提出的问题和用户响应,以便我可以将其保存到数据库的 question/answer 对中。

我可以使用 ng-model 获取输入的值,但是,我不确定如何获取问题。该解决方案不能要求从 js 数组中读取问题。它们必须通过内容小部件输入到标记中。

我正在考虑类似 Label for 的行为,<span> 的 ng-model 行为类型或向包含所问问题的控件添加属性。

谢谢。

    <span ng-model="user.favoriteColor.label">Favorite color:</span>
    <input type="text" id="favoriteColor" ng-model="user.favoriteColor">

这是一个基本示例的代码: Plunk

我的目标是最终得到一个包含问题和响应的 <span>label 值的对象。当我 post:

时它看起来像这样
    [
    {questionText: 'Favorite color?', answer: 'red'},
    {questionText: 'Favorite food?', answer: 'pizza'},
    etc...
    ]

这里是 Plunk 我是如何解决这个问题的。

    <span id="q1Label">Question 1:</span>
    <input type="text" id="q1" ng-model="questions[0].questionResponse" ng-blur="getQuestionAndAnswer($event,0)" />

    $scope.questions=[];
    $scope.getQuestionAndAnswer = function(event, arrayIndex) {
    var questionId = event.target.id;
    var questionLabelId = questionId + "Label";

    $scope.questions[arrayIndex].questionId = questionId;

    var questionText = document.getElementById(questionLabelId).textContent;
    $scope.questions[arrayIndex].questionText = questionText;

    $scope.questions.push({
    'questionText':questionText,
    'questionResponse': scope.questions[arrayIndex].questionResponse,
    'questionId': questionId
    });
    }