0

I have a $scope variable in my controller. Initially it will load a value.

My requirement is that I want to update $scope variable value when button click.

Please let me know how can i achieve this.I am newbie to Angular.

var userName='UserName';

   tepTableModule.controller('tepTableCtrl',function($scope,Service,$filter) {
       $scope.searchText=userName;
       $scope.refreshData = function() {

//when refreshData function called i want to update $scope.searchText value   to new value.
     $scope.tableData.data = $filter('filter')(tepJobData, $scope.searchText, undefined);
       };
    }
3
  • paste your html snippet as well Commented Dec 2, 2015 at 11:36
  • 2
    does $scope.searchText = newValue; not work? Commented Dec 2, 2015 at 11:49
  • @ Claies yes it is working. Commented Dec 2, 2015 at 11:57

2 Answers 2

2

You can make function and call it on ng-click:

on Controller:

$scope.doSomething = function (){
//do something
}

On View:

<button ng-click="doSomething()">Click Me</button>

That's it, good luck

Sign up to request clarification or add additional context in comments.

2 Comments

Thats not entirely correct. You have to define your method of your $scope or this if using controller as. This code as it stands won't execute anything as your method is just defined as a normal function.
yes, you are right and that what I meant, anyways, thanks and I will update my answer.
1

You make use of the ng-click directive.

Its a directive that will execute a method in your controller when the user clicks on the element. It us not limited to just methods, it will evaluate an angular expression, so can be used to set flags on elements, increment counters etc.

So

<!--Execute Method-->
<button ng-click="refreshData()">Refresh</button>
<!--toggle Scope property isDisabled-->
<button ng-click="isDisabled = !isDisabled">toggle disabled</button>
<!--increment count by 1-->
<button ng-click="count += 1">Add 1</button>

With controller as

$scope.refreshData = function () {
    //epic refreshes
}
$scope.isDisabled = false;
$scope.count = 0;

docs: https://docs.angularjs.org/api/ng/directive/ngClick

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.