在AngularJS中使用Helper
我正在使用laravel和angularjs开发一个社交应用程序 我有一种情况,当我使用helper检查用户关系时在AngularJS中使用Helper,angularjs,angularjs-scope,angular-scenario,Angularjs,Angularjs Scope,Angular Scenario,我正在使用laravel和angularjs开发一个社交应用程序 我有一种情况,当我使用helper检查用户关系时 **Html** //showing users other info <div ng-repeat="user in data"> <a ng-click=checkuser(user.id,checkrelation(user.id))> {=checkrelation(user.id) =}
**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;
}
}
**Html**
//向用户显示其他信息
{=checkrelation(user.id)=}
**js**
//保存用户数据的范围
//我使用访问用户的服务获取此信息
$scope.data=myservice.userinfo();
//使用服务保存用户当前订户信息的范围
$scope.allsubscriber=myservice.usersubscriber();
//检查用户关系并一致返回文本
$scope.checkrelation=函数(id){
//如果用户发现
if($scope.allsubscriber.indexOf(id)!=1){
返回“伙伴”;
}否则{
//找不到
返回“订阅”;
}
}
$scope.checkuser=函数(id,文本){
如果(文本==“订阅”){
//进行ajax调用以保存用户订阅服务器
//在这里,我想重新加载由helper检查的用户状态
}否则
返回false;
}
}
现在,若用户不是当前用户的朋友,那个么它将被相应地添加。现在的问题是,当用户单击{=checkrelation(data.id)=}时,我如何反映{=checkrelation(data.id)=}中的更改,而不进行页面重新加载,因为在页面加载时,我可以看到文本上的更改。任何帮助都是有帮助的。角度模板为您提供
$event
,您可以将其传递到回调中。这允许您执行$event.preventDefault()
,这将覆盖该默认单击
例如:
<a href="#/whatevz" ng-click="myFunc(thing1, $event)">link text</a>
编辑:
preventDefault()
是DOM规范的一部分preventDefault
实际上是DOM规范的一部分;jQuery和jqlite都不需要使用它。
$scope.myFunc = function(thing, e){
e.preventDefault();
/*do stuff with thing*/
}