最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing scope variable to function in ng-click - Stack Overflow

programmeradmin12浏览0评论

So I know I must be missing some basic part of javascript with this quesiton but here it goes anyhow:

I have a contoller that has a variable declared in it:

$scope.IsStartDatePickerHidden = true;
$scope.IsEndDatePickerHidden = true;

I have a button that has its ng-click set to go to a function in my controller. This function would be nice if it took in a parameter which it can change the value:

<button type="button" ng-click="showDatePicker(IsStartDatePickerHidden)" >

 $scope.showDatePicker = function(showDatePicker)
 {
     showDatePicker = false;
 }

when I step through this code the function showDatePicker changes the value of the parameter that is passed in but does not seem to change the value of the variable in the controller, so nothing happens. I am sure this has to be something to do with passing by reference. I am just not sure how to pass this in so that $scope.IsStartDatePickerHidden or $scope.IsEndDatePickerHidden are changed depending on which one I pass in.

So I know I must be missing some basic part of javascript with this quesiton but here it goes anyhow:

I have a contoller that has a variable declared in it:

$scope.IsStartDatePickerHidden = true;
$scope.IsEndDatePickerHidden = true;

I have a button that has its ng-click set to go to a function in my controller. This function would be nice if it took in a parameter which it can change the value:

<button type="button" ng-click="showDatePicker(IsStartDatePickerHidden)" >

 $scope.showDatePicker = function(showDatePicker)
 {
     showDatePicker = false;
 }

when I step through this code the function showDatePicker changes the value of the parameter that is passed in but does not seem to change the value of the variable in the controller, so nothing happens. I am sure this has to be something to do with passing by reference. I am just not sure how to pass this in so that $scope.IsStartDatePickerHidden or $scope.IsEndDatePickerHidden are changed depending on which one I pass in.

Share Improve this question asked Sep 17, 2014 at 20:20 user3648646user3648646 7112 gold badges16 silver badges24 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

Brad's answer is correct, but just in case you wanted something for more general use, so that you can change the value of any scope variable:

<button type="button" ng-click="showDatePicker('IsStartDatePickerHidden')" >

 $scope.showDatePicker = function(varName)
 {
     $scope[varName] = false;
 }

This is not necessarily good practice, but often useful. Speaking of which, you could just do the assignment inside the ng-click:

<button type="button" ng-click="IsStartDatePickerHidden = false" >

The how-to-fix-that-example Fiddle: http://jsfiddle/p3p6ova7/

If you know the variables you want to work with on your scope, there is no need to pass in those values. In your case, you were initializing a new variable within your functon, showDatePicker, and assigning false to that, which wouldn't have any effect on $scope.IsStartDatePickerHidden

You could also simply do ng-click="IsStartDatePickerHidden = false", but that may be a little outside best practice.

发布评论

评论列表(0)

  1. 暂无评论