0

I'm trying to edit an element in real time by triggering a function on ng-click with AngularJS.

Here's my html:

<div class="row question">{{questions.1.name}} <a href="" class="glyphicon glyphicon-pencil" ng-click="editQuestion(questions.1.name)"></a></div>

And the js:

function QuestionsMap($scope) {
$scope.questions = {
"1": {
  "name": "Hello! Do you like to travel?",
  "ID": "1",
  "answer": {
    "yes": {
      "name": "Yes",
    },
    "no": {
      "name": "No",
    }
  }
}

};
$scope.editQuestion = function (name) {
  $scope.editing = $scope.questions[name];
};
}

What am I missing? Is the editQuestion function not properly written?

2
  • What happens when the ng-click triggers now? Commented Mar 1, 2014 at 16:53
  • What value are you trying to store inside of $scope.editing? Commented Mar 1, 2014 at 17:01

3 Answers 3

2

$scope.questions[name] isn't anything. There is no $scope.questions["Hello, do you like to travel"]. There is however a $scope.questions["1"]. Try passing in questions.1.ID instead of questions.1.name


Also @c-smile is right. Don't start your keys with integers. That is not allowed in js. For this to work you need to switch to something like 'n1'

Sign up to request clarification or add additional context in comments.

1 Comment

I've updated the html to ng-click="editQuestion(questions.n1.ID)", renamed to n1, and rewritten the function to $scope.editQuestion = function (name) { $scope.editing = $scope.questions["n1"]; }; And still nothing :S
1

input field wont show up magically you have to code that.

<input type="text" ng-model="newquestion"><input type="button" ng-click="add()" value="save">

controller, you can specify what you want to push inside like $scope.questions.push({'name':$scope.newquestion.name,'id':$scope.newquestion.id});

$scope.add = function(){
    $scope.questions.push($scope.newquestion);
    $scope.newquestion = "";    
};

Comments

1

This questions.1.name is incorrect syntax from JS perspective. Name of the variable in JS cannot start from digit. So you shall refactor your code to something like this:

<div class="row question">{{questions.n1.name}}

and so

"n1": { ... }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.