AngularJS处理ng-view之外的数据

时间:2014-03-22 10:02:16

标签: angularjs

在我的AngularJS app index.html页面中,我有三个主要的div(顶部,页脚和),根据当前路线显示不同的视图。我现在面临的问题是我需要在页脚区域显示一些数据,包括需要循环的数据(例如,州列表,城市......等),但我不确定如何在索引中显示数据。 ng-view之外的html。有人能告诉我这是如何实现的吗?任何示例代码都非常受欢迎。感谢

下面是我的index.html结构,App.js

app.js

var myApp = angular.module('myApp', ['ngRoute', 'ngSanitize']);
myApp.config(['$routeProvider', function($routeProvider) {

  $routeProvider.when('/', 
  {     templateUrl: 'templates/home.html', 
        controller: 'HController',
        title: 'Home',

  });

}]);

的index.html

  <html lang="en" ng-app="myApp">
  <head>...</head>
  <div id="header">....</div>
  <div ng-view></div>
  <div id="header">
     <!-- Here is where I need to loop to display data -->
  </div>

1 个答案:

答案 0 :(得分:1)

使用ngRouteng-view并不排除在代码中使用ng-controller和其他角度构造,只要代码位于具有ng-app属性的元素内(或已经bootstrapped)。

因此,由于ng-app上有html属性,您可以定义一个新控制器并在#header中使用它:

<强>的index.html:

<div id="header" ng-controller="headerController">
   {{myData}}
   <!-- Here is where I need to loop to display data -->
</div>

<强> app.js:

myApp.controller('headerController', 
    [ '$scope', function ($scope) { 
        $scope.myData = 'Stuff.'; 
    }]
);
相关问题