21

I'm trying to use ngSanitize so that I can inject html content ( external) into my app. However I got stuck at the ngSanitize installation. If I add it into my app module it stops working . Should I download this from somewhere ? Angularjs docs don't say anything.

var crmApp = angular.module('crmApp', [
  'ngRoute',
  'crmControllers',
  'ui.bootstrap',
  'ngSanitize',
]);
hey
  • 7,299
  • 14
  • 39
  • 57
  • After installing bower install angular-sanitize --save need to import ngsanitize in angular.module('crmApp', [ 'ngRoute', 'crmControllers', 'ui.bootstrap', 'ngSanitize', ]).??? – Varun Sharma Feb 09 '16 at 07:16

4 Answers4

27

Install bower. You want bower.

npm install -g bower

Go to your project root and install ngSanitize:

bower install angular-sanitize --save

bower install downloads the script for you. Include the JavaScript in your app:

<script src="/bower_components/angular-sanitize/angular-sanitize.js"></script>
Andy Gaskell
  • 31,495
  • 6
  • 74
  • 83
  • 1
    hello ... the way you installed this ... was this for nodejs?? I mean if I am doing just good old fashioned html with javascript, this doesn't apply to me does it?? – dsdsdsdsd Mar 27 '16 at 05:19
  • Bower is a package manager that is mostly used on the client side. That said, Bower falling out of favor a little bit. I would consider using NPM to manage dependencies. It'd be a good idea to understand what a package manger is even if you end up not using one right now. – Andy Gaskell Mar 27 '16 at 05:29
  • well I have been playing with node, even bought a book 3 weeks ago (only read chapter 1 so far) ... but using it occurs WITHIN the node environment, whereas when I am building a good ole traditional site with traditional ` – dsdsdsdsd Mar 27 '16 at 17:58
  • 1
    Package managers are relevant on the client side. Do some reading on package managers are used in a general sense. Keep going, I think it will click in time. Let's say someone else built `some_file.js`. Every time they release a new version do you want to have to visit the project's web site and download it and then update your project? I don't! Bower/NPM will do the boring stuff for you. – Andy Gaskell Mar 27 '16 at 19:15
14

If you want to use CDN resources without download go to https://code.angularjs.org/ and click on version you want .Will find all the various resources available there and then you can copy location straight into script tag:

<script src="//code.angularjs.org/1.2.20/angular-sanitize.min.js"></script>
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • This is discouraged to use in production environments. Read about it here: https://docs.angularjs.org/api/ngSanitize – Detilium May 03 '17 at 08:55
2

install npm install --save angular-sanitize

import in app module import ngSanitize from "angular-sanitize";

add it to your module angular.module("app", [ngSanitize])

use in component

controller: class appController {
  constructor($sanitize) {
    this.$sanitize = $sanitize;
    this.text = this.$sanitize('text to be sanitized');
  }
}
BeeBee8
  • 2,944
  • 1
  • 27
  • 39
1

Yes, you need to download and include it: https://github.com/angular/bower-angular-sanitize

Jon Snow
  • 3,682
  • 4
  • 30
  • 51