使用指令的angular js中的Like和different

Like and unlike in angular js using directive

本文关键字:中的 Like different js angular 指令      更新时间:2023-09-26

Html

<a href="javascript:;" class="replycommentbtn ng-binding" 
   ng-click="likeClick(uniqueid)"> Like (63)</a> &nbsp;| &nbsp; 
<a href="javascript:;" class="replycommentbtn ng-binding" 
   ng-click="unlikeClick(uniqueid)"> Unlike (14)</a>

指令

angular.module('AlbumlikeFeature',[]).directive('AlbumlikeFeature', ['$filter', '$route', function ($filter, $route) {
    return {
      restrict: 'E',
      scope: {},
      templateUrl: 'app/components/album/albumlikefeature.html';           
 scope.likeClick = function (dataid) {      
      var resultlike = dataid
      console.log(resultlike);
      resultlike.Like = resultlike.Like + 1;               
      scope.likeCount = resultlike.Like;
      scope.unlikeCount = resultlike.Unlike;             
 };

如何在其他控制器中调用我的指令模块?。我试过这样的<albumlike-feature uniqueid="1" photoid="1"></albumlike-feature>,但没有结果。

我的要求是点击,就像我想增加值一样,而不像same想增加值。我怎样才能做到这一点?

Like(1)。。。想要在like和same as different中增加计数值。

我在点击类似按钮时出现此错误

ReferenceError: likeCount is not defined
    at Scope.scope.likeClick (albumlikefeature.js:15)
    at Parser.functionCall (angular.js:10795)
    at angular.js:19036
    at Scope.$get.Scope.$eval (angular.js:12632)
    at Scope.$get.Scope.$apply (angular.js:12730)
    at HTMLAnchorElement.<anonymous> (angular.js:19035)
    at HTMLAnchorElement.n.event.dispatch (jquery.min.js:3)
    at HTMLAnchorElement.n.event.add.r.handle (jquery.min.js:3)

指令名称必须跟在camelCase后面,因此在指令中更改名称:

directive('AlbumlikeFeature',

将成为

directive('albumlikeFeature',

现在,在您看来,将其称为:

<albumlike-feature uniqueid="1" photoid="1"></albumlike-feature>

还有你的指令功能是错误的,

templateUrl: 'app/components/album/albumlikefeature.html';
 scope.likeClick = function (dataid) {
            var resultlike = dataid
            console.log(resultlike);
            resultlike.Like = resultlike.Like + 1;               
            scope.likeCount = resultlike.Like;
            scope.unlikeCount = resultlike.Unlike;             
        };

返回对象中不能有;,也没有link函数。

angular.module('AlbumlikeFeature',[]).directive('albumlikeFeature', ['$filter', '$route', function ($filter, $route) {
return {
    restrict: 'E',
    scope: {},
    templateUrl: 'app/components/album/albumlikefeature.html',
    link : function(scope, element, attrs) {
      scope.likeClick = function (dataid) {
        var resultlike = dataid
        console.log(resultlike);
        resultlike.Like = resultlike.Like + 1;               
        scope.likeCount = resultlike.Like;
        scope.unlikeCount = resultlike.Unlike;             
      };
    }           
 }
}]);

使用此

在点击之前,你需要将ur值定义为scope.likeCount = 0,并且与之相同。。。

只需使用+=1来增加值。这将对您有效。。

angular.module('albumlikeFeature',[]).directive('albumlikeFeature', ['$filter', '$route', function ($filter, $route) {
return {
    restrict: 'E',
    scope: {},
    templateUrl: 'app/components/album/albumlikefeature.html',
    link : function(scope, element, attrs) {
        scope.likeCount = 0;
        scope.unlikeCount = 0;
        scope.likeClick = function (dataid) {
        scope.likeCount += 1;          
      };
       scope.unlikeClick = function (dataid) {
        scope.unlikeCount += 1;          
      };
    }           
 }
}]);