最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - what is the alternative of angular.isString() in angular2? - Stack Overflow

programmeradmin3浏览0评论

I am working on a project in angular2 and curious to know if there is any mean by which I can use angularjs functionalities in my angular2 application.

for ex.

in angularjs, I used to do following operations:

  1. angular.isString(value)
  2. angular.isArray(value)
  3. angular.copy(value)

I just want to know that is there any module or package which can help me do above operations in angular2/typescript?

Thanks in advance.

I am working on a project in angular2 and curious to know if there is any mean by which I can use angularjs functionalities in my angular2 application.

for ex.

in angularjs, I used to do following operations:

  1. angular.isString(value)
  2. angular.isArray(value)
  3. angular.copy(value)

I just want to know that is there any module or package which can help me do above operations in angular2/typescript?

Thanks in advance.

Share Improve this question asked Jun 8, 2016 at 6:43 Bhushan GadekarBhushan Gadekar 13.8k21 gold badges88 silver badges131 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 15

Just use JavaScript:

  • isString

Simple

  typeof foo === 'string'
  • angular.isArray(value)

Simple

Array.isArray(value)
  • angular.copy(value)

Simple

Object.assign({},value)

Except for copy, angular2 actually provides the isString and isArray (and a lot more) functions from "@angular/common/src/facade/lang". To use these you have to import them like this:

import {isString, isArray} from "@angular/common/src/facade/lang";

But, the body of these functions are the same as basarat mentioned, and this import is no longer available. Sooo, use the solution above :)

You could use lodash-es (ES module import support for lodash) to do the following:

import { isString } from 'lodash-es';

console.log(isString('') === true);

I prefer this over the accepted answer of typeof foo === 'string' because string literals are prone to mistakes and are harder to minify.

发布评论

评论列表(0)

  1. 暂无评论