【问题标题】:Cannot binding click in shell.html and shell.js durandal无法在 shell.html 和 shell.js durandal 中绑定单击
【发布时间】:2013-11-01 18:21:21
【问题描述】:

这是我的源代码:

main.js:

requirejs.config({
    paths: {
        'text': '../Scripts/text',
        'transitions': '../Scripts/durandal/transitions',
        'durandal': '../Scripts/durandal',
        'plugins': '../Scripts/durandal/plugins',
        'knockout': '../Scripts/knockout-2.3.0',
        'jquery': '../Scripts/jquery-1.10.2.min',
        'nivoLightbox': '../Scripts/nivo-lightbox/nivo-lightbox'
    },
    shim: {
        'bootstrap': {
            deps: ['jquery'],
            exports: 'jQuery'
        }
    }
});

define(['durandal/system', 'durandal/app', 'durandal/viewLocator'], function (system, app, viewLocator) {
    //>>excludeStart("build", true);
    system.debug(true);
    //>>excludeEnd("build");
    app.title = 'Cu?i di ku';
    //specify which plugins to install and their configuration
    app.configurePlugins({
        router: true,
        dialog: true,
        widget: {
            kinds: ['expander']
        }
    });
    app.start().then(function () {
        //Replace 'viewmodels' in the moduleId with 'views' to locate the view.
        //Look for partial views in a 'views' folder in the root.
        viewLocator.useConvention();
        //Show the app by setting the root view model for our application.
        app.setRoot('shell', 'entrance');
    });
});

在我的shell.html,我有菜单:

<div id="top_nav" class="ddsmoothmenu">
    <ul>
        <li data-bind="click: goHome()"><a class="selected"></a></li>
    </ul>
    <br style="clear: left" />
</div>

shell.js:

define(['plugins/router', 'durandal/app'], function (router, app) {
    return {
        router: router,
        activate: function () {
            router.map([
                { route: ['', 'home'], title: 'Mới nhất', moduleId: 'viewmodels/stories', nav: true },
                { route: ':storyId/:story', title: ':storyId', moduleId: 'viewmodels/story', nav: true }
            ]).buildNavigationModel();

            return router.activate();
        },
        goHome: function () {
            router.navigate('');
        }
    };
});

第一次加载页面时,它运行到goHome()函数(我不知道为什么?)。 当我点击菜单时,它不执行这个功能?请帮我解决这个问题。

【问题讨论】:

    标签: knockout.js durandal


    【解决方案1】:

    尝试替换:

    <li data-bind="click: goHome()"><a class="selected"></a></li>
    

    with: (goHome 后没有 ())

    <li data-bind="click: goHome"><a class="selected"></a></li>
    

    第一次应用绑定时,() 调用 goHome 函数。

    如果这仍然不能解决问题,当页面加载或单击 li 时,您是否在控制台中收到任何错误?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 2014-07-05
      • 2015-07-09
      • 2014-11-12
      相关资源
      最近更新 更多