TemplateUrl angularJS无效

时间:2015-02-11 18:47:43

标签: javascript angularjs templates directive

我遇到像这样的templateUrl问题 我的目录

myapp
    js
        home.js
    templates
        profile.html

我在home.js中尝试这个。

.state('app.profile', {
    url : '/profile',
    views : {
        'appContent' : {
            templateUrl: '/templates/profile.html',
            controller: 'ProfileController'
        }
    }
})

在我的个人资料中我有

<ion-view>
    <ion-content padding="true">
        <div>
            <p>My content</p>
        </div>
    </ion-content>
</ion-view> 

它不起作用,但是当我尝试

.state('app.profile', {
    url : '/profile',
    views : {
        'appContent' : {
            template: '<ion-view><ion-content padding="true"><div><p>My content</p></div></ion-content></ion-view>',
            controller: 'ProfileController'
        }
    }
})

它正常工作。怎么了?我该如何解决?

2 个答案:

答案 0 :(得分:0)

您需要做的就是在模板前删除额外的/

.state('app.profile', {
url : '/profile',
views : {
    'appContent' : {
        templateUrl: 'templates/profile.html',
        controller: 'ProfileController'
    }
}})

答案 1 :(得分:0)

您可以在脚本顶部导入模板,并将其用作“模板”,而不是“ templateUrl”:

import profile from '/templates/profile.html';
...

.state('app.profile', {
url : '/profile',
views : {
    'appContent' : {
        template: profile ,
        controller: 'ProfileController'
    }
  }
})