question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Require function in AngularJS templates

See original GitHub issue

I want use require function inside template, there’s an example of usage:

<div data-ng-include="require('myApp/templates/directive.html')"></div>

After processing of this template by wepback, module with this template will look like this:

function(module, exports) {
    var path = '/some/prefix/path/myApp/templates/directive.html';
    window.angular.module('ng').run(['$templateCache', function(c) {
        c.put(path, "<div data-ng-include=\"require('myApp/templates/directive.html')\"></div>")
    }]);
    module.exports = path;
}

But require function should be replaced by webpack_require function and the result module should be like this:

function(module, exports) {
    var path = '/some/prefix/path/myApp/templates/directive.html';
    window.angular.module('ng').run(['$templateCache', function(c) {
        c.put(path, "<div data-ng-include=\"'" + __webpack_require__(123) + "'\"></div>")
    }]);
    module.exports = path;
}

Additionally, require function can be used not only in ng-include, ng-inline directives, it can be used in other custom attributes also.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
k-stasevichcommented, Jul 16, 2016

“ngtemplate-loader”: “1.3.1”,

/**

  • build templates form angular */ { test: /.html$/, loader: “ngtemplate?relativeTo=” + (path.resolve(__dirname, ‘./src/app’)) + “/!html” }

<ng-include src=“‘components/header/navigation/navigation.html’”></ng-include> // WORK <ng-include src=“‘./components/header/navigation/navigation.html’”></ng-include> // NOT WORK <ng-include `src=“‘/components/header/navigation/navigation.html’”></ng-include> // NOT WORK

project root: src/app/components/header/navigation/navigation.html

i think you can close this issue

0reactions
WearyMonkeycommented, Aug 1, 2016

Closing as out of scope.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tutorial: 2 - AngularJS Templates
View and Template​​ In AngularJS, the view is a projection of the model through the HTML template. This means that whenever the model...
Read more >
Function of require in angularjs
1 Answer 1 · The require takes a string name (or array of strings) of the directive(s) to pass in. · If an...
Read more >
AngularJS component & templates
AngularJS components have two ways to set a template for component. By passing path with templateUrl, passing string or function thetemplate ...
Read more >
AngularJS to Angular concepts: Quick reference
In AngularJS, the ng-switch directive swaps the contents of an element by selecting one of the templates based on the current value of...
Read more >
Loading AngularJS Components With RequireJS After ...
Simply loading and executing the "lazy" JavaScript file will register the JavaScript components. The HTML templates, on the other hand, require ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found