5

I have a component as following and I am trying to provide my svg viewBox dimension dynamically, injected from my bootstrap in main.ts.

import {Component} from 'angular2/core';
import {CircleComponent} from './circle.component';
import {Circles} from './circles.service';

@Component({
    selector: 'my-app',
    styleUrls: ['./app/app.component.css'],
    directives: [CircleComponent],
    providers: [Circles],    
    template: `
        <svg [attr.viewBox]="getViewBox()" preserveAspectRatio="xMidYMid meet">
            <svg:g my-circle *ngFor="#circle of circles.circles" [circle]="circle" />
        </svg>
    `
})
export class AppComponent{
    static parameters = [Circles, 'canvasWidth', 'canvasHeight'];

    constructor(circles: Circles, canvasWidth: Number, canvasHeight: Number) {
        this.circles = circles;
        this.width = canvasWidth;
        this.height = canvasHeight;
    }    

    function getViewBox(): String {
        return `0 0 ${this.width} ${this.height}`;
    }
}

My main.ts

import {provide}        from 'angular2/core';
import {bootstrap}      from 'angular2/platform/browser'
import {AppComponent}   from './app.component'

bootstrap(AppComponent, [
    provide('canvasWidth', {useValue: 900}),
    provide('canvasHeight', {useValue: 300})
]);

And the exception I get

EXCEPTION: TypeError: l_context.getViewBox is not a function in [getViewBox() in AppComponent@1:13]

I'm not sure it's a good way to do this thing, but this information is needed elsewhere in my circles.service. One thing I don't like though is that I cannot specify the Number type so I have to define static parameters = [Circles, 'canvasWidth', 'canvasHeight']; in AppComponent.

1 Answer 1

3

Remove function from your method declaration, It will look like below.

getViewBox(): String {
    return `0 0 ${this.width} ${this.height}`;
}

Basically what happen behind the scene is when you write class method with function, transpiled JavaScript throw that function outside of that prototype function returned by the name of class. That's why it become unreachable.

In this case you would have get error at compile, if you are using typescript with better tool like Visual Studio/Web Strom.

export class App{
    function test(){
        console.log();
    }
}

Transpiled As

"use strict";
var App = (function () {
    function App() {
    }
    return App;
}());
exports.App = App;
function test() {
    console.log();
}

Code Link

Sign up to request clarification or add additional context in comments.

6 Comments

oh, I thought it was the way to define a function, you're right, it's working when I remove it...
I have another function in another class and if I remove the function keyword, that one is failing...
I read your full answer and I understand why my other case was falling, I had to use this.functionName() but I was using functionName() without this, then with the function keyword, It could be found some ways...
do you think it's a good way to inject the canvas width/height like I do or I should use another method? I should maybe create a new question
@JCorriveau I think lets open up new.. we can't really relate these both issue together.. Thanks :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.