【问题标题】:Using Helper in AngularJS在 AngularJS 中使用 Helper
【发布时间】:2014-04-19 13:36:44
【问题描述】:

我正在使用 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)=} 的变化,因为在页面加载时我cam 看到文本的变化。任何帮助都会有所帮助。

【问题讨论】:

    标签: angularjs angularjs-scope angular-scenario


    【解决方案1】:

    Angular 模板为您提供$event,您可以将其传递给您的回调。这允许您执行 $event.preventDefault(),这将覆盖默认的 点击。

    示例:

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

    然后在你的控制器中:

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

    编辑preventDefault() 是 DOM 规范的一部分

    【讨论】:

    • preventDefault 实际上是 DOM 规范的一部分; jQuery 和 jqlite 都不需要使用它。
    猜你喜欢
    • 1970-01-01
    • 2013-04-01
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    相关资源
    最近更新 更多