Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将空格替换为数据绑定中的-in 页面标题: 页面别名:_Javascript_Angularjs - Fatal编程技术网

Javascript 将空格替换为数据绑定中的-in 页面标题: 页面别名:

Javascript 将空格替换为数据绑定中的-in 页面标题: 页面别名:,javascript,angularjs,Javascript,Angularjs,我不熟悉angular并使用简单的数据绑定,因此每当用户输入页面标题时,别名都会自动生成,但我希望标识空格并用“-”(破折号)替换它们。例如:每当用户进入主页时,我希望别名是主页,如果是主页则更好。 我试过这么做 <form role="form" method="POST" action="{{ url('/roles/save') }}" data-ng-app=""> <div class="form-group"> <label>

我不熟悉angular并使用简单的数据绑定,因此每当用户输入页面标题时,别名都会自动生成,但我希望标识空格并用“-”(破折号)替换它们。例如:每当用户进入主页时,我希望别名是主页,如果是主页则更好。 我试过这么做

  <form role="form" method="POST" action="{{ url('/roles/save') }}" data-ng-app="">    
<div class="form-group">
      <label>Page-Title:</label>
       <input type="text" required value="" data-ng-model="title" name="page_title" class="form-control">                     
</div>

<div class="form-group">
 <label>Page-Alias:</label>
 <input type="text" value="@{{ title }}" name="page_alias" class="form-control">
 </div>

您可以为模板创建筛选器:

 <input type="text" value="@{{title.replace(/-/g, ' ');}} name="page_alias" class="form-control">
并使用它:

.filter('replacementFilter', function() {
   return function(input) {
       return input.replace(/ /g, '-');
   }
});

在这里查看:


有两种方法:一种是使用表达式中的
替换,另一种是使用角度过滤器

 <input type="text" value="@{{ title | replacementFilter }}" name="page_alias" class="form-control">

如果您想启用文本框的模糊功能,可以通过以下方式使用:

value="@{{ title | replaceSpaceWithDash : '-' | lowercase }}"
更新:

angular.module('app', [])
.controller('myController', function($scope) {
$scope.removeSpaces = function(text) {   

   $scope.title =  text.split(' ').join('-').toLowerCase();
  }
});
“我要识别空格并用“-”(破折号)替换它们” JS

angular.module('app', [])
.controller('myController', function($scope) {
$scope.removeSpaces = function(text) {   

 $scope.alias =  text.split(' ').join('-').toLowerCase();
}
});
HTML

angular.module('app')
    .filter('slugify', function() {
        return function(input) {
            input = input || '';

            return input.replace(/ /g, '-').toLowerCase();
       }
    });

“如果是主页就更好了”
我在替换后添加了
toLowerCase
,以实现此目的。

您的示例将破折号替换为空格。他/她有不同的描述和示例:)。在示例中,尝试用空格替换破折号,但在描述中则相反。如果是这样的话,就替换那里的值。这是因为在这个例子中,xenish说他们尝试了,但没有成功。文章标题和内容表明了目的。您的第一个示例是用空格替换破折号。另一方面,您的替换语法应该是相反的。@xenish,您认为我的答案被接受了吗?如果是这样的话,你能这样标记吗?
   <input type="text" required value="" data-ng-model="title" ng-blur="removeSpaces(title)" name="page_title" class="form-control">  
   <input type="text" value="@{{ alias }}" name="page_alias" ng-modal="alias" class="form-control">
angular.module('app', [])
.controller('myController', function($scope) {
$scope.removeSpaces = function(text) {   

 $scope.alias =  text.split(' ').join('-').toLowerCase();
}
});
angular.module('app')
    .filter('slugify', function() {
        return function(input) {
            input = input || '';

            return input.replace(/ /g, '-').toLowerCase();
       }
    });
 <input type="text" value="@{{ title | slugify }}" name="page_alias" class="form-control">