2014-04-19 44 views
0

我正在使用laravel和angularjs開發社交應用程序。在AngularJS中使用助手

我有一種情況,當我檢查用戶關係使用助手。

**Html** 


    //showing users other info  

    <div ng-repeat="user in data"> 
    <a ng-click=checkuser(user.id,checkrelation(user.id))> 
     {=checkrelation(user.id) =} <a> 
    </div> 

**js** 

//scope to hold user's data 
//i get this info using service for the visiting user 
$scope.data = myservice.userinfo(); 

//scope to hold users current subscriber info using service 
$scope.allsubscriber = myservice.usersubscriber(); 

//check user relation and return text accordinglly 
$scope.checkrelation = function(id){ 
//if user found 
if($scope.allsubscriber.indexOf(id) != 1){ 
    return "fellow"; 
}else{ 
// not found 
return "subscribe"; 
} 

} 

$scope.checkuser = function(id,text){ 
    if(text === "subscribe"){ 
    //make ajax call to save user subscriber 
    //here i want to reload the user status which is checked by helper 
}else 

return false; 

    } 
    } 

現在,如果用戶不是當前用戶的朋友,然後它會被添加accordingly.Now的問題是如何能反映{= checkrelation(data.id)=}變化用戶點擊它,而不進行頁面重載因爲在頁面加載時,我會在文本上看到chnage。任何幫助將是有益的。

回答

0

有角模板爲您提供$event,您可以將其傳入您的回調。這允許你做$event.preventDefault(),它將覆蓋默認的< a/>點擊。

例如:

<a href="#/whatevz" ng-click="myFunc(thing1, $event)">link text</a> 

然後在你的控制器:

$scope.myFunc = function(thing, e){ 
    e.preventDefault(); 
    /*do stuff with thing*/ 
} 

編輯preventDefault()是DOM規範的一部分

+1

'preventDefault'實際上是DOM規範的一部分; jQuery和jqlite都不需要使用它。 –

+0

酷 - thx - 回答編輯 –