1

我正在尝试创建主从应用程序,但无法访问模板中的控制器 $scope 变量。这是我的 html 的样子:

<body >   

    <ons-tabbar var='Apptabbar'>
      <ons-tabbar-item icon="users" label="People" active="true" page="people.html"></ons-tabbar-item>
      <ons-tabbar-item icon="clock-o" label="Schedule"page="schedule.html"></ons-tabbar-item>
      <ons-tabbar-item icon="map-marker" label="Map" page="map.html"></ons-tabbar-item>
    </ons-tabbar>

<script type="text/ons-template" id="people.html">
<ons-navigator>
  <ons-page ng-controller="PeopleController">
    <ons-toolbar modifier="bgred">
      <div class="center">People</div>
    </ons-toolbar>

    <ons-list>
      <ons-list-item modifier="chevron" ng-repeat="person in persons" ng-click="showDetail($index)">
      {{ person.first_name }} {{ person.last_name }}
      </ons-list-item>
    </ons-list>
  </ons-page>
</ons-navigator>
</script>

<script type="text/ons-template" id="person.html">
  <ons-page ng-controller="PersonController">
    <ons-toolbar modifier="bgred">
      <div class="left">
        <ons-back-button style="color: #FFF;">People</ons-back-button>
      </div>
      <div class="center">{{person.first_name}}</div>
    </ons-toolbar>

    <ons-list>
      <ons-list-item>{{person.first_name}}</ons-list-item>
    </ons-list>
  </ons-page>
</script>

</body>

我的 js 文件看起来像这样

(function(){
    'use strict';
    var app = angular.module('tedxph', ['onsen.directives']);
    //Person Controller
    app.controller('PersonController', function ($scope, $data) {
        $scope.person = $data.selectedItem;
        console.log($scope.person)
    })
    //People Controller
    app.controller('PeopleController', function ($scope, $data) {
        $scope.persons = $data.items;

        $scope.showDetail = function (index) {
            var selectedItem = $data.items[index];
          $data.selectedItem = selectedItem;
          $scope.ons.navigator.pushPage('person.html', {title : selectedItem.first_name});
        }
    })

    app.factory('$data', function() {
        var data = {};

        data.items = [
            {first_name: "Steve", last_name: "Jobs"},
            {first_name: "Larry", last_name: "Page"},
            {first_name: "Bill", last_name: "Gates"},
            {first_name: "Micheal", last_name: "Dell"}
        ];

        return data;
    });
})();

person.html 页面不呈现变量值,而是显示“{{person.first_name}}”而不是值。难道我做错了什么?

4

1 回答 1

0

尝试将 JSON 密钥用双引号括起来。下划线可能有一些打嗝。

于 2014-08-11T22:24:10.790 回答