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);
};
}
2 Answers 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
2 Comments
$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.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;
$scope.searchText = newValue;not work?