静态内容的ng-include与ui-view

时间:2016-04-06 10:11:57

标签: angularjs angular-ui-router angularjs-ng-include

按照惯例,我通过angular填充我的index.html。我的索引看起来像:

<body>
  <nav ng-include="'app/partials/navbar.html'" ng-controller="NavBarController"></nav>
  <main>
    <section ui-view="donate"></section>
    <section ng-include="'app/partials/about.html'"></section>
    <section ui-view="stories"></section>
    <section ng-include="'app/partials/contact.html'"></section>
  </main>
  <footer ng-include="'app/partials/footer.html'"/>
</body>

我的nav,关于,联系人和<footer>都有静态内容,因此我使用了ng-include。捐赠和故事<section>是动态的,因此我使用ui-view

问题

使用ui-view优于静态内容的ng-include是否有任何优势? nav ui-view可能会更好NavBarController我可以引用$stateProvider.state()中的function verify1() { jQuery.ajax({ type: 'POST', url:"functions.php", data: new FormData($("#infoForm1")[0]), processData: false, contentType: false, success: function(returnval) { $("#show1").html(returnval); $('#show1').show(); } }); } ,但静态部分会怎样?

1 个答案:

答案 0 :(得分:3)

仅当您想要利用浏览器历史记录功能时,

ui-view才有用。对于例如如果您有以下HTML

<div class="searchbar">
    <input type="text" ng-model="student.id">
    <button type="button" ng-click="getStudent()">Get Student</button>
</div>
<div class="student-info">
    <div class="student" ui-view="studentview"></div>
</div>

在这里有一个ui-view是有意义的,因为我们可以将不同的数据(如学生ID等)作为参数传递给同一个模板并显示不同的内容。此外,浏览器历史记录将帮助我们在不同的学生之间导航。

对于关于页脚等内容,虽然这些内容大部分是静态的,但我会建议您使用ng-include ,因为您几乎无法获得任何内容额外的路由器在这里。

对于联系,它可能取决于它包含的内容。如果它是需要导航的东西(比如每个国家/地区办公室的联系路线),那么请使用ui-route,否则坚持使用ng-include