2014-03-01 23 views
0

我试图通过用AngularJS在ng-click上触发一个函数来实时编辑元素。Angularjs不会奇迹般地创建输入元素进行编辑

这里是我的html:

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

而且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]; 
}; 
} 

我缺少什么? editQuestion函数编写不正确?

+0

当NG-点击触发器现在会发生什么呢? – C1pher

+0

什么也没有。 –

+0

你想在$ scope.editing里存储什么值? – C1pher

回答

1

输入字段不会显示神奇你必须编码。

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

控制器,你可以指定要内推像$scope.questions.push({'name':$scope.newquestion.name,'id':$scope.newquestion.id});

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

这个questions.1.name从JS角度来看是不正确的语法。 JS中变量的名称不能从数字开始。所以,你应重构代码,以这样的:

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

"n1": { ... } 
2

$scope.questions[name]没有什么。没有$scope.questions["Hello, do you like to travel"]。然而有一个$scope.questions["1"]。尝试在questions.1.ID传球questions.1.name


,而不是同时@ C-微笑是正确的。不要用整数开始你的钥匙。这在js中是不允许的。为了这个工作,你需要切换到像'n1'

+0

我已将html更新为ng-click =“editQuestion(questions.n1.ID)”,重命名为n1,并将函数重写为 $ scope.editQuestion = function(name){ $ scope.editing = $ scope.questions [ “N1”]; }; 而仍然没有:S –

相关问题