【问题标题】:listener for ons-sliding-menu "preopen" eventons-sliding-menu "preopen" 事件的监听器
【发布时间】:2014-09-30 14:16:52
【问题描述】:

我正在尝试为 ons-sliding-menu "preopen" 事件定义事件侦听器。我希望在预打开时调用 UICtrl 控制器的 setTitle() 函数。这是我的代码

app.js

app.controller('UICtrl', function($scope, UIService){  

  $scope.setMainTitle = function(title) {
    UIService.setTitle(title);
  }  
 }

我尝试了以下 HTML。但是事件甚至没有被触发(我试图写到控制台,它根本没有被触发)

index.html

<div ng-controller="UICtrl" >
    <ons-sliding-menu  var="app.slidingMenu"  ng-preopen="setMainTitle('test')"
   menu-page="menu.html" main-page="main.html"  swipable 
    side="left" type="reveal" max-slide-distance="250px">
</ons-sliding-menu>    
 </div>

我尝试的另一件事是: index.html

 ons.ready(function() {
    app.slidingMenu.on('preopen', function() {
        console.log('preopen');
        setMainTitle('Test');
    } )
 });

在这种情况下,事件被触发,但 setMainTitle 函数在此范围内自然是未定义的。

有人可以建议如何实现这一目标吗?

【问题讨论】:

    标签: onsen-ui


    【解决方案1】:

    我猜标题在你的 main.html 上??

    我尝试了同样的事情,但没有使用 angularJS

    看看这个样本: codepen example

    JS:

    ons.bootstrap();
    
    ons.ready(function() {
      menu.on("preopen", function() {
        document.getElementById("title").innerHTML = "my title";
      });
    });
    

    HTML:

    <ons-sliding-menu
                      above-page="page1.html"
                      behind-page="menu.html"       
                      side="left"
                      max-slide-distance="250px"
                      var="menu">
    </ons-sliding-menu>
    
    <ons-template id="page1.html">
      <ons-page>
        <ons-toolbar>
          <div class="left">
            <ons-toolbar-button ng-click="menu.toggleMenu()"><ons-icon icon="ion-navicon" style="font-size: 32px; width: 1em;"></ons-icon></ons-toolbar-button>
          </div>
          <div class="center" id="title">Page 1</div>
        </ons-toolbar>
    
        <p style="text-align: center; color: #999; padding-top: 100px;">Page1 Contents</p>
      </ons-page>
    </ons-template>
    
    
    <ons-template id="page2.html">
      <ons-page>
        <ons-toolbar>
          <div class="left">
            <ons-toolbar-button onclick="menu.toggleMenu()"><ons-icon icon="ion-navicon" style="font-size: 32px; width: 1em;"></ons-icon></ons-toolbar-button>
          </div>
          <div class="center">Page 2</div>
        </ons-toolbar>
    
        <p style="text-align: center; color: #999; padding-top: 100px;">Page2 Contents</p>
      </ons-page>
    </ons-template>
    
    <ons-template id="menu.html">
      <ons-list>
        <ons-list-item modifier="chevron" onclick="menu.setAbovePage('page1.html', {closeMenu: true})">
          page1.html
        </ons-list-item>
        <ons-list-item modifier="chevron" onclick="menu.setAbovePage('page2.html', {closeMenu: true})">
          page2.html
        </ons-list-item>
      </ons-list>
    </ons-template>
    

    【讨论】:

    • 是的工具栏在 index.html 中。我正在努力使其成为“全球性”并且对整个应用程序都一样。但是我到目前为止找不到解决方案。我现在使用存储工具栏数据的角度服务。但是 UI 没有更新。您认为这至少可以通过 onsen 和 angular 实现吗?
    • 链接中的代码不是您需要实现的吗?也许我没有 100% 理解你的问题。
    • 在您的示例中,每个模板中都有工具栏。这东西我已经有了。我正在考虑在 index.html 中使用单个工具栏。并且模板中没有工具栏,就像这样。但我认为这行不通:` ..... ..... .... ons-page> `
    • 这样不行。每个模板都是一组全新的页面......不仅仅是上一页的一部分......他们不能共享任何组件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多