TemplateUrl angularJS not working

TemplateUrl angularJS not working

本文关键字:working not angularJS TemplateUrl      更新时间:2023-10-09

我有这样的templateUrl问题我的目录

myapp
    js
        home.js
    templates
        profile.html

我在home.js 中尝试过

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

在我的个人资料.html中,我有

<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'
        }
    }
})

它工作正常。怎么了?我该怎么修?

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

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

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

import profile from '/templates/profile.html';
...
.state('app.profile', {
url : '/profile',
views : {
    'appContent' : {
        template: profile ,
        controller: 'ProfileController'
    }
  }
})