从NG-模型传递价值NG-单击功能 [英] passing value from ng-model to ng-click function
问题描述
我有< TD>
看起来像这样:
<td>
<input type="text" ng-repeat="letter in letters(cron_format) track by $index" value="{{letter}}" ng-model="letter"/>
<button class="save" ng-click="saveCron(user_id,script_id,cron_format,letter)">save</button>
</td>
在值={{信}}
和NG-模型=信都包含正确的值内,但是当我将它传递给saveCron()函数像这样的:
the value="{{letter}}
and ng-model="letter" both contain the correct value inside but when i pass it to the saveCron() function like this:
$scope.saveCron = function(userId,scriptId,cronFormat,data){
console.log(data);
}
我得到undefined..why是什么?
i get undefined..why is that?
推荐答案
为什么信
未定义的原因是,有没有信
在您的控制器的范围。
The reason why letter
is undefined is that there is no letter
in your controller's scope.
这时你可能会问,为什么&LT;输入类型=文本NG重复=,在信信(cron_format)轨道由$指数值={{信}}NG-模型=信/&GT;
可以工作?
Then you may ask, why <input type="text" ng-repeat="letter in letters(cron_format) track by $index" value="{{letter}}" ng-model="letter"/>
can work?
这是因为 NG-重复
将创建每个元素的单独的子范围,并创建一个名为信对象
中这个范围。然后 NG-模型
可以读取它。然而,它不是在父范围(控制器的范围内)进行访问。
It's because ng-repeat
will create a separate child scope for each element and create an object called letter
in this scope. Then ng-model
can read it. However it's not accessible in the parent scope (your controller's scope).
希望我的解释是明确的。
Hope my explanation is clear.
这篇关于从NG-模型传递价值NG-单击功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!