templateUrl by variable

3,924 views
Skip to first unread message

Long Field

unread,
Jan 11, 2016, 7:57:39 PM1/11/16
to AngularJS
Have try a lot, couldn't find out how to do this: templateUrl dynamically from a variable/property,  [Url], {{URL}}, "[Url]", "{{Url}}", none of them work

@Component({
templateUrl:  Url
})
export class App {
public Url  = "app/about.html";
 }

michael corbridge

unread,
Jan 11, 2016, 9:04:03 PM1/11/16
to AngularJS
I'm pretty sure you have to declare that var outside the class. Let me try and I'll let you know.

Sander Elias

unread,
Jan 11, 2016, 11:19:43 PM1/11/16
to AngularJS
Hi Long,

You have to declare that variable before you define your class. At the time the @component decorator runs, your class is not there yet.

Regards
Sander

Long Field

unread,
Jan 12, 2016, 12:08:32 AM1/12/16
to AngularJS
Hi Sander
I need my class to get data from backend and then I can know which templateUrl to use?
Or should I use @input in templateUrl ?

Günter Zöchbauer

unread,
Jan 12, 2016, 3:01:38 AM1/12/16
to AngularJS

Long Field

unread,
Jan 12, 2016, 6:05:21 PM1/12/16
to AngularJS


On Tuesday, January 12, 2016 at 7:01:38 PM UTC+11, Günter Zöchbauer wrote:
  It looks like a missing function, follow doc. both template and tenplateUrl as ?string but why we can use {{url}} inside template string but not templateUrl string?

Günter Zöchbauer

unread,
Jan 14, 2016, 8:54:00 AM1/14/16
to AngularJS
`templateUrl` points to a file containing a string where you can use binding/{{}}, you can't for `templateUrl` itself. It just points to a file.
`template` is for inline template and there you can use binding/{{}}.

Long Field

unread,
Jan 14, 2016, 10:26:19 PM1/14/16
to AngularJS
I knew I cann't, just not happy this limitation, I wanta it point to different files according to data read from backend

Sander Elias

unread,
Jan 18, 2016, 8:39:45 AM1/18/16
to AngularJS
Hi Long,

Then read the data before initializing the directive. If this is crucial to your app, you can use xmlHttpRequest, or Fetch to get the data before you directive gets initialized.

Regards
Sander

Arslan Munir

unread,
May 18, 2020, 12:52:05 PM5/18/20
to Angular and AngularJS discussion
In my case, i wanted to use two different html templates with different css frameworks. Bulma and bootstrap. Just for testing purposes. I switched the templates based on the environment variable.

In environment file

export const environment = {
  useBulma: true
};
and in component

import { environment } from 'src/environments/environment';

@Component({
      selector: 'app-test',
      templateUrl: environment.useBulma ? './with-bulma.component.html' : './with-bootstrap.component.html',
      styleUrls: ['./value.component.css']
    })
Reply all
Reply to author
Forward
0 new messages