2015-12-02 97 views
0

我的控制器中有一個$scope變量。最初它會加載一個值。

我的要求是,我想更新$scope當點擊按鈕時的變量值。

請讓我知道如何實現這一點。我是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); 
     }; 
    } 
+0

粘貼HTML片段以及 – Vineet

+2

不'$ scope.searchText = NEWVALUE;'不行? – Claies

+0

@ Claies是的,它工作。 – gihan

回答

1

您使用ng-click指令。

它是一個指令,當用戶點擊元素時將在您的控制器中執行一個方法。它我們不僅限於方法中,將評估的角度表達,所以可用於設置上的元素標誌,增量計數器等

所以

<!--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> 

隨着控制器作爲

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

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

2

可以使函數並調用它NG點擊:

控制器上:

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

上查看:

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

就是這樣,運氣好的話

+0

那不完全正確。如果使用'controller as',你必須定義'$ scope'或'this'的方法。由於您的方法只是定義爲普通函數,因此此代碼不會執行任何操作。 – ste2425

+1

是的,你是對的,而我的意思,反正,謝謝,我會更新我的答案。 – user2120121

相關問題