传递$范围变量的函数在AngularJS [英] passing $scope variable to a function in AngularJS
问题描述
我一直在试图建立在角通用函数
改变一个 $范围
参数值。
I've been trying to create a generic function
in angular that changes a $scope
parameters value.
我一直在想,为什么我不能通过 $ scope.var
为参数
到函数
例如:
I've been wondering why I cannot pass $scope.var
as an argument
to a function
e.g.:
webApp.controller ('VotesCtrl', function ($scope) {
$scope.param = 42;
change($scope.param);
function change(contextParam) {
contextParam = (Math.random()*100)+1;
};
});
当我跑了这个功能 $ scope.param
42仍然
When I ran this function $scope.param
remains 42.
是否有其他外:
webApp.controller ('VotesCtrl', function ($scope) {
$scope.param = 42;
change('param');
function change(contextParam) {
$scope[contextParam] = (Math.random()*100)+1;
};
});
推荐答案
简短的回答是不绑定到原始值。见<一href=\"http://stsc3000.github.io/blog/2013/10/26/a-tale-of-frankenstein-and-binding-to-service-values-in-angular-dot-js/\">http://stsc3000.github.io/blog/2013/10/26/a-tale-of-frankenstein-and-binding-to-service-values-in-angular-dot-js/
The short answer is "Don't bind to primitive values". See http://stsc3000.github.io/blog/2013/10/26/a-tale-of-frankenstein-and-binding-to-service-values-in-angular-dot-js/
var webApp = angular.module('webApp', []);
//controllers
webApp.controller ('VotesCtrl', function ($scope) {
$scope.param = { value: 42 };
change($scope.param);
function change(contextParam) {
contextParam.value = (Math.random()*100)+1;
};
});
这篇关于传递$范围变量的函数在AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!