0

我正在使用 laravel 和 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;

  }
  } 

现在,如果用户不是当前用户的朋友,那么它将被相应地添加。现在的问题是我如何在用户点击它而不重新加载页面时反映 {=checkrelation(data.id)=} 中的变化,因为在页面加载时我会看到 chnages在文本上。任何帮助都会有所帮助。

4

1 回答 1

0

Angular 模板为您提供$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 规范的一部分

于 2014-04-19T17:42:29.403 回答