评估$ scope赋值

时间:2014-04-26 17:01:37

标签: angularjs angularjs-scope

对不起,我会直接来这里。 我在我的控制器中有这个功能。

$scope.btnOptionSelect = function(id, value, target_id, target_value)  
{
    target_id = id;
    target_value = value;
}

我有这个片段来调用函数:

<button class="btnSelect" ng-click="btnOptionSelect(1,'Machinery',machine_edit.division_id, machine_edit.division_name)">Select</button>

我正在尝试将待target_id分配为id而将target_value分配为value分别为:

$scope.machine_edit.division_id = id;
$scope.machine_edit.division_name = value;

我不知道如何在AngularJS中执行此操作。 请帮忙。谢谢。

2 个答案:

答案 0 :(得分:0)

您可以直接指定

$scope.btnOptionSelect = function(target_id, target_value)  
{
    target_id = $scope.machine_edit.division_id;
    target_value = $scope.machine_edit.division_name;
}

答案 1 :(得分:0)

我建议您直接在方法中设置范围变量:

$scope.btnOptionSelect = function (id, value) {
    $scope.machine_edit.division_id = id;
    $scope.machine_edit.division_name = value;
}

或者你需要在你的功能中传递它们吗?

更新小提琴: http://jsfiddle.net/HB7LU/3275/ 这里目标值作为参数传递。当它是传递给函数的对象值时会有点棘手,因为它们是副本而不是直接引用对象中的值。但是您可以将对象作为目标传递,将目标值作为键传递。如果您不传入任何目标对象,该函数还支持直接设置target_vals。

相关问题