Angularjs 角形减速器在末端留下破折号

Angularjs 角形减速器在末端留下破折号,angularjs,Angularjs,删除重音和符号,但在末尾留下破折号 我也会在控制器之间共享相同的功能,怎么做 代码: 例如: 资料来源:内马尔是最好的球员 return: neymar-the-best-player- [已解决] 我发现了这一点:为了让它公开,有一个必要的步骤来归还一些东西。然后可以将其包装为常量,并注入任何控制器 app.controller('MainCtrl', function($scope, slugify) { $scope.show = function(){ alert(slu

删除重音和符号,但在末尾留下破折号

我也会在控制器之间共享相同的功能,怎么做

代码:

例如:

资料来源:内马尔是最好的球员

 return: neymar-the-best-player-
[已解决]


我发现了这一点:

为了让它公开,有一个必要的步骤来归还一些东西。然后可以将其包装为
常量
,并注入任何控制器

app.controller('MainCtrl', function($scope, slugify) {
  $scope.show = function(){
    alert(slugify($scope.someText));
  }
});
app.constant('slugify', function(slug){
    str = slug.replace("-");
    str = angular.lowercase(str);
    str = str.replace(/[^A-Z0-9]+/ig, "-");        
    return  str;
});
此外,从函数应用的转换的性质来看,它符合
过滤器的模式

app.filter('slugifyFilter', function(){
  return function(slug){
    str = slug.replace("-");
    str = angular.lowercase(str);
    str = str.replace(/[^A-Z0-9]+/ig, "-");        
    return  str;
  }
});
并在模板中使用类似的方法

 {{someText | slugifyFilter}}

请在

中查看它的实际操作。要将其公开,有必要返回某些内容。然后可以将其包装为
常量
,并注入任何控制器

app.controller('MainCtrl', function($scope, slugify) {
  $scope.show = function(){
    alert(slugify($scope.someText));
  }
});
app.constant('slugify', function(slug){
    str = slug.replace("-");
    str = angular.lowercase(str);
    str = str.replace(/[^A-Z0-9]+/ig, "-");        
    return  str;
});
此外,从函数应用的转换的性质来看,它符合
过滤器的模式

app.filter('slugifyFilter', function(){
  return function(slug){
    str = slug.replace("-");
    str = angular.lowercase(str);
    str = str.replace(/[^A-Z0-9]+/ig, "-");        
    return  str;
  }
});
并在模板中使用类似的方法

 {{someText | slugifyFilter}}

请在

中查看它的实际操作。要将其公开,有必要返回某些内容。然后可以将其包装为
常量
,并注入任何控制器

app.controller('MainCtrl', function($scope, slugify) {
  $scope.show = function(){
    alert(slugify($scope.someText));
  }
});
app.constant('slugify', function(slug){
    str = slug.replace("-");
    str = angular.lowercase(str);
    str = str.replace(/[^A-Z0-9]+/ig, "-");        
    return  str;
});
此外,从函数应用的转换的性质来看,它符合
过滤器的模式

app.filter('slugifyFilter', function(){
  return function(slug){
    str = slug.replace("-");
    str = angular.lowercase(str);
    str = str.replace(/[^A-Z0-9]+/ig, "-");        
    return  str;
  }
});
并在模板中使用类似的方法

 {{someText | slugifyFilter}}

请在

中查看它的实际操作。要将其公开,有必要返回某些内容。然后可以将其包装为
常量
,并注入任何控制器

app.controller('MainCtrl', function($scope, slugify) {
  $scope.show = function(){
    alert(slugify($scope.someText));
  }
});
app.constant('slugify', function(slug){
    str = slug.replace("-");
    str = angular.lowercase(str);
    str = str.replace(/[^A-Z0-9]+/ig, "-");        
    return  str;
});
此外,从函数应用的转换的性质来看,它符合
过滤器的模式

app.filter('slugifyFilter', function(){
  return function(slug){
    str = slug.replace("-");
    str = angular.lowercase(str);
    str = str.replace(/[^A-Z0-9]+/ig, "-");        
    return  str;
  }
});
并在模板中使用类似的方法

 {{someText | slugifyFilter}}
请参见主要问题中的操作:

删除重音和符号slagify,但在末尾保留破折号

在我们讨论主要问题之前,代码中有几个部分需要修改

  • 第一条
    replace()
    语句适用于任何其他字符串之间的破折号,但如果破折号放在字符串的两端,则肯定会有问题
  • e、 g.
    '-一些文本-
    结果到
    'undefinedSome-textnefined'

    要解决此问题,需要使用空字符串添加
    replace()
    的第二个参数

    发件人:

    致:

  • 第二个
    replace()
    语句有一个正则表达式标志,表示
    i
    ,表示不区分大小写的搜索。虽然正则表达式语句建议使用
    A-Z
    (大写表达式),但这实际上是多余的,因为您已经将字符串修改为小写。因此,该声明应该更改:
  • 发件人:

    致:

  • 现在,对于主要问题,您只是缺少字符串两端的破折号修剪函数 在#2中的代码后添加此replace语句


    从我在代码中看到的情况来看,这似乎更像是一个可重用的函数,而不是可以归因于控制器函数的函数。您可以为该特定函数创建一个服务,并将其注入控制器中

    app.controller('MainCtrl', function($scope, slugify) {
      $scope.show = function(){
        alert(slugify($scope.someText));
      }
    });
    app.constant('slugify', function(slug){
        str = slug.replace("-");
        str = angular.lowercase(str);
        str = str.replace(/[^A-Z0-9]+/ig, "-");        
        return  str;
    });
    

      .controller('SluggerController', function($scope, slugify) {
    
        $scope.slug = 'Neymar the best player!';
        $scope.item = { slug: '' };
    
        $scope.slugify = function(slug) {
          $scope.item.slug = slugify(slug);
        };
    
      })
    
      .factory('slugify', function() {
        return function(text) {
          return angular.lowercase(text)
            .replace('-', '')
            .replace(/[^a-z0-9]+/g, '-')
            .replace(/^-+|-+$/g, '');
        };
      });
    
    更新:

    由于您不希望将unicode字符作为虚线字符包含,因此可以将其与#2合并

      .controller('SluggerController', function($scope, slugify) {
    
        $scope.slug = 'Neymar the best player!';
        $scope.item = { slug: '' };
    
        $scope.slugify = function(slug) {
          $scope.item.slug = slugify(slug);
        };
    
      })
    
      .factory('slugify', function() {
        return function(text) {
          return angular.lowercase(text)
            .replace('-', '')
            .replace(/[^a-z0-9]+/g, '-')
            .replace(/^-+|-+$/g, '');
        };
      });
    
    而不是:

    str = str.replace(/[^a-z0-9]+/g, "-");
    
    将其更改为:

    str = str.replace(/[^\u00BF-\u1FFF\u2C00-\uD7FF\w0-9]/g, '-');
    
    至于我是如何得到正则表达式的,您可以参考以下主要问题:

      .controller('SluggerController', function($scope, slugify) {
    
        $scope.slug = 'Neymar the best player!';
        $scope.item = { slug: '' };
    
        $scope.slugify = function(slug) {
          $scope.item.slug = slugify(slug);
        };
    
      })
    
      .factory('slugify', function() {
        return function(text) {
          return angular.lowercase(text)
            .replace('-', '')
            .replace(/[^a-z0-9]+/g, '-')
            .replace(/^-+|-+$/g, '');
        };
      });
    
    删除重音和符号slagify,但在末尾保留破折号

    在我们讨论主要问题之前,代码中有几个部分需要修改

  • 第一条
    replace()
    语句适用于任何其他字符串之间的破折号,但如果破折号放在字符串的两端,则肯定会有问题
  • e、 g.
    '-一些文本-
    结果到
    'undefinedSome-textnefined'

    要解决此问题,需要使用空字符串添加
    replace()
    的第二个参数

    发件人:

    致:

  • 第二个
    replace()
    语句有一个正则表达式标志,表示
    i
    ,表示不区分大小写的搜索。虽然正则表达式语句建议使用
    A-Z
    (大写表达式),但这实际上是多余的,因为您已经将字符串修改为小写。因此,该声明应该更改:
  • 发件人:

    致:

  • 现在,对于主要问题,您只是缺少字符串两端的破折号修剪函数 在#2中的代码后添加此replace语句


    从我在代码中看到的情况来看,这似乎更像是一个可重用的函数,而不是可以归因于控制器函数的函数。您可以为该特定函数创建一个服务,并将其注入控制器中

    app.controller('MainCtrl', function($scope, slugify) {
      $scope.show = function(){
        alert(slugify($scope.someText));
      }
    });
    app.constant('slugify', function(slug){
        str = slug.replace("-");
        str = angular.lowercase(str);
        str = str.replace(/[^A-Z0-9]+/ig, "-");        
        return  str;
    });
    

      .controller('SluggerController', function($scope, slugify) {
    
        $scope.slug = 'Neymar the best player!';
        $scope.item = { slug: '' };
    
        $scope.slugify = function(slug) {
          $scope.item.slug = slugify(slug);
        };
    
      })
    
      .factory('slugify', function() {
        return function(text) {
          return angular.lowercase(text)
            .replace('-', '')
            .replace(/[^a-z0-9]+/g, '-')
            .replace(/^-+|-+$/g, '');
        };
      });
    
    更新:

    由于您不希望将unicode字符作为虚线字符包含,因此可以将其与#2合并

      .controller('SluggerController', function($scope, slugify) {
    
        $scope.slug = 'Neymar the best player!';
        $scope.item = { slug: '' };
    
        $scope.slugify = function(slug) {
          $scope.item.slug = slugify(slug);
        };
    
      })
    
      .factory('slugify', function() {
        return function(text) {
          return angular.lowercase(text)
            .replace('-', '')
            .replace(/[^a-z0-9]+/g, '-')
            .replace(/^-+|-+$/g, '');
        };
      });
    
    而不是:

    str = str.replace(/[^a-z0-9]+/g, "-");
    
    将其更改为:

    str = str.replace(/[^\u00BF-\u1FFF\u2C00-\uD7FF\w0-9]/g, '-');
    
    至于我是如何得到正则表达式的,您可以参考以下主要问题:

      .controller('SluggerController', function($scope, slugify) {
    
        $scope.slug = 'Neymar the best player!';
        $scope.item = { slug: '' };
    
        $scope.slugify = function(slug) {
          $scope.item.slug = slugify(slug);
        };
    
      })
    
      .factory('slugify', function() {
        return function(text) {
          return angular.lowercase(text)
            .replace('-', '')
            .replace(/[^a-z0-9]+/g, '-')
            .replace(/^-+|-+$/g, '');
        };
      });
    
    删除重音和符号slagify,但在末尾保留破折号

    在我们讨论主要问题之前,代码中有几个部分需要修改

  • 第一条
    replace()
    语句适用于任何其他字符串之间的破折号,但如果破折号放在字符串的两端,则肯定会有问题
  • e、 g.
    '-一些文本-
    结果到
    'undefinedSome-textnefined'

    要解决此问题,需要使用空字符串添加
    replace()
    的第二个参数

    发件人:

    致:

  • 第二个
    replace()
    语句有一个正则表达式标志,表示
    i
    ,表示不区分大小写的搜索。而正则表达式语句建议使用
    A-Z
    (大写expre