小编PSL*_*PSL的帖子

删除字符串中重复单词的出现次数

以下面的字符串为例:

var string = "spanner, span, spaniel, span";
Run Code Online (Sandbox Code Playgroud)

从这个字符串我想找到重复的单词,删除所有重复项,保持单词出现一次,然后输出修改后的字符串.

在这个例子中将是:

var string = "spanner, span, spaniel";
Run Code Online (Sandbox Code Playgroud)

我已经设置了一个jsFiddle进行测试:http://jsfiddle.net/p2Gqc/

请注意,字符串中单词的顺序不一致,每个字符串的长度也不一致,因此正则表达式不会在这里完成工作我不这么认为.我正在考虑将字符串拆分成数组的方法吗?但我希望它尽可能轻松地在客户端上,超级快速......

javascript arrays string jquery

11
推荐指数
2
解决办法
4万
查看次数

为什么Jquery只影响第一个div元素?

我正在使用"替换"功能删除div中的所有非数字值.

似乎Jquery替换只影响第一个元素.

这是我的Jquery:

$('#comment').each(function() {
    var thz = $(this);
    var repl = thz.html(thz.html().replace(/\D+/g, ''));
});
Run Code Online (Sandbox Code Playgroud)

HTML代码:

<a id="comment1" href="#"> c2f?f011. </a>
<a id="comment1" href="#"> c20ff113. </a>
<a id="comment1" href="#"> c201gf76341. </a>
Run Code Online (Sandbox Code Playgroud)

结果:

2011 c20ff113.c201gf76341.

我想要的结果是:

2011 20113 20176341

html javascript jquery

10
推荐指数
3
解决办法
3433
查看次数

为什么使用自定义标签创建元素会在IE9或10中的outerHTML中添加xml命名空间,直到调用.find()方法为止?

我有一个jsfiddle,演示了这个问题:

http://jsfiddle.net/H6gML/8/

$(document).ready(function() {

    // this seems fine in IE9 and 10
    var $div = $("<div>");
    console.log("In IE, this <div> is just fine: " + $div[0].outerHTML);

    // this is weird in IE
    var $test = $("<test>");    
    console.log("However, this <test> has an xml tag prepended: \n" 
                + $test[0].outerHTML);    
    $test.find("test");    
    console.log("Now, it does not: \n" + $test[0].outerHTML);    
    console.log("Why does this behave this way?");
});
Run Code Online (Sandbox Code Playgroud)

为什么会这样?它不会发生在Chrome或Firefox中.有没有更好的方法来解决这个问题,而不是调用.find("test")对象?

编辑

为了澄清,我不是在问为什么添加xml标签,而是,我想知道为什么.find()调用可以摆脱它.这对我来说没有意义.

javascript jquery internet-explorer dom

10
推荐指数
1
解决办法
638
查看次数

AngularJS和Typescript - 注入服务

我一直在写AngularJS应用程序已经有一段时间了,但是Typescript对我来说是新的,然后将AngularJS添加到Typescript中与我习惯的有点不同.

无论如何,两者有什么区别:

app.service('customerDataService', Application.Services.CustomerDataService);

app.service('customerDataService', ['$http', '$q', Application.Services.CustomerDataService]);

控制器TS

module Application {
    export module Services {
        export interface ICustomerDataService {
            getCustomer(id: number): ng.IPromise<Models.ICustomer>;
        }

        export class CustomerDataService implements ICustomerDataService {
            constructor(private $http: ng.IHttpService, private $q: ng.IQService) {
            }

            getCustomer(id: number): ng.IPromise<Models.ICustomer> {
                return this.$http.get('data/Customer.json').then((response) => {
                    return response.data;
                });
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

App TS

var app = angular.module('app', []);

app.value('config', new Application.ApplicationConfig());

app.service('customerDataService', ['$http', '$q', Application.Services.CustomerDataService]);
app.service('customerDataService', Application.Services.CustomerDataService);

app.controller('DefaultController', ['$scope','config', 'customerDataService', Application.Controllers.DefaultController]);
Run Code Online (Sandbox Code Playgroud)

他们似乎都工作.您是否必须明确定义服务的注入?

javascript angularjs typescript angularjs-service

10
推荐指数
1
解决办法
2万
查看次数

Karma错误参数'Controller'不是函数,未定义

我试图测试我的控制器时遇到了问题.当我运行测试时出现错误

Error: [ng:areq] Argument 'MainCtrl' is not a function, got undefined   http://errors.angularjs.org/1.3.8/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined
        at assertArg (/Users/tetianachupryna/project/bower_components/angular/angular.js:1577)
        at assertArgFn (/Users/tetianachupryna/project/bower_components/angular/angular.js:1588)
        at /Users/tetianachupryna/project/bower_components/angular/angular.js:8418
        at /Users/tetianachupryna/project/src/spec/controllers/main-controller.spec.js:11
        at /Users/tetianachupryna/project/src/spec/controllers/main-controller.spec.js:17
        at /Users/tetianachupryna/project/node_modules/karma-jasmine/lib/adapter.js:184
        at http://localhost:9877/karma.js:185
        at http://localhost:9877/context.html:51
Run Code Online (Sandbox Code Playgroud)

我知道SO充满了类似的问题.但是我在Angular和JS中总体上是空的,所以这些答案对我没有帮助.从SO上的类似问题我发现我的问题是控制器的错误定义,但我仍然无法弄清楚我做错了什么.我已经堆叠了,我求你帮忙.

首先,这是我的src/app/index.js文件,其中定义了我的模块

var app = angular.module('myModule', [
  'ngAnimate',
  'ngSanitize',
  'ngResource',
  'ui.router',
  'pascalprecht.translate',
  'thing1',
  'thing2']);
Run Code Online (Sandbox Code Playgroud)

这是src/app/controllers/main-controller.js

angular.module('myModule').controller('MainCtrl', [
    '$scope',
    '$state',
    function ($scope, $state) {
      $scope.state = $state;
      //***
      $scope.isBigStep = function isBigStep() {
        return $state.$current.step == 3;
      };    
  }]);
Run Code Online (Sandbox Code Playgroud)

最后这个文件带有测试src/spec/controllers/main-controller.spec.js

describe('MainCtrl', function() {
  var scope, $state, createController; …
Run Code Online (Sandbox Code Playgroud)

javascript jasmine angularjs karma-runner karma-jasmine

10
推荐指数
1
解决办法
1万
查看次数

未知提供者:$ rootElementProvider使用$ injector在angular.bootstrap之前获取$ location服务

您好我正在尝试手动引导角度应用程序,但有一些业务需要首先处理.文章中提到我感兴趣的技术.

当我注入这个:

 var $injector = angular.injector(["ng"]);
 var $http = $injector.get("$http");
Run Code Online (Sandbox Code Playgroud)

它工作正常,但这:

var $injector= angular.injector(["ng", "myApp"]);
var $location = $injector.get("$location");
Run Code Online (Sandbox Code Playgroud)

引发以下错误.

Uncaught Error: [$injector:unpr] Unknown provider: $rootElementProvider <- $rootElement <- $location <- $location
Run Code Online (Sandbox Code Playgroud)

是否有可能在angular.bootstrap之前获得$ location?

万分感谢!

javascript angularjs angularjs-injector

10
推荐指数
1
解决办法
6349
查看次数

为什么这个CSS3在高度上的过渡不起作用

我一直在手风琴中工作,但由于一些奇怪的原因,下面的代码:

<style>
    .collapse {
    position: relative;
    height: 0;
    overflow: hidden;
    -webkit-transition: height .35s ease;
    -moz-transition: height .35s ease;
    -o-transition: height .35s ease;
    transition: height .35s ease;
    }
    .collapse.in {
    height: auto;
    }
</style>

<div class="accordion">
    <div class="something">
        <a class="" >Accordion Pane 1</a>
    </div>
    <div class="accordion-body collapse">
        <div class="accordion-inner"> content </div>
    </div>
</div>

<script>

$('.something').click(function(event){
    event.preventDefault();
    $(this).next('.accordion-body').toggleClass('in');

})


</script>
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/CvdXK/

似乎不起作用.高度没有动画.我不知道为什么.

首先十分感谢.

css jquery css3 transitions

9
推荐指数
1
解决办法
1万
查看次数

AngularJS [$ injector:unpr]未知的提供者

我试图将服务注入控制器,我收到以下错误:

Error: [$injector:unpr] Unknown provider: employeeServiceProvider <- employeeService
http://errors.angularjs.org/1.3.0-beta.17/$injector/unpr?p0=employeeServiceProvider%20%3C-%20employeeService
    at http://localhost:9082/angularJSDemo/js/lib/angular.js:78:12
    at http://localhost:9082/angularJSDemo/js/lib/angular.js:3894:19
    at Object.getService [as get] 
Run Code Online (Sandbox Code Playgroud)

这是代码的plunker.任何帮助,将不胜感激.

angularjs angularjs-service

9
推荐指数
1
解决办法
4万
查看次数

如何根据用户输入的$ viewValue更新来更新ngModel的$ modelValue

说我有以下指令:

myApp.directive('myDirective', function() {
    return {
        restrict: 'A',
        require: 'ngModel',
        scope: {
            ngModel: '='
        },
        link: function(scope, elem, attrs, ngModelCtrl) {
            scope.$watch('ngModel', function() {
                ngModelCtrl.$modelValue = 'foo';
            });
        }
    }
}); 
Run Code Online (Sandbox Code Playgroud)

以下html:

<input ng-model="name" my-directive></input>
Run Code Online (Sandbox Code Playgroud)

基本上,每当用户更改输入时,my-directive理想情况下将内部模型值更改为"foo",同时保持视图值不变.

但是当我$scope.name在相应的控制器中打印出来时,它不记录"foo",它会记录用户输入的内容.

看起来这ngModelCtrl.$modelValue不是控制器正在访问的 - 我是不正确地接近这个问题?

(另外观察ngModel范围感觉确实是错误的,但我不确定是否有任何其他方式.任何建议都会非常感激!)

angularjs angularjs-directive angular-ngmodel

9
推荐指数
1
解决办法
1万
查看次数

使用Asp.Net Web应用程序中的sysinternals PSExec执行脚本

我试图PSExec从我的Asp.Net Web应用程序执行连接到远程服务器.它"Access Denied Error -5"以某种方式提供没有凭据设置,并通过设置它给出的PSEXEC命令中的凭据 "2250 Network connection could not be found".我是服务器上的管理员,我已Windows authentication and Asp.Net Impersonation启用(IIS7.5).更有趣的是,当我尝试从一个console application或甚至只是使用command prompt它执行它只是工作正常.我试图做一个ping操作作为测试.

这是我的代码片段: -

            var startInfo = new ProcessStartInfo{
                CreateNoWindow = true,
                UseShellExecute = false,
                FileName = FilePath,
                Arguments = CommandArgs
            }

            Process vsCommandProcess = Process.Start(startInfo);

            vsCommandProcess.WaitForExit();
            var exitCode = vsCommandProcess.ExitCode;
            if (vsCommandProcess.ExitCode != 0)
            {
                ...rest of the code
Run Code Online (Sandbox Code Playgroud)

这里:-

FilePath --> C:\pstools\psexec.exe
Arguments --> \\servername -accepteula -u domain\userName -p …
Run Code Online (Sandbox Code Playgroud)

c# asp.net process processstartinfo psexec

8
推荐指数
1
解决办法
3090
查看次数