【问题标题】:Angular for DataBind Only -ng-init仅用于 DataBind 的 Angular -ng-init
【发布时间】:2014-05-27 22:23:38
【问题描述】:

在我的页面顶部我有

================================================ ================================

<html lang="en" data-ng-app="app">
<head>
    <style>
        /* This helps the ng-show/ng-hide animations start at the right place. */
        /* Since Angular has this but needs to load, this gives us the class early. */
        .ng-hide {
            display: none !important;
        }
    </style>

================================================ ==============================

在我身体的更深处。

================================================ ==============================

<div class="row row-offcanvas row-offcanvas-left" data-ng-controller="dashboard" data-ng-init="vm=dashboard()">

================================================ ===

我想在起始页的正文中呈现以下内容

================================================ ===

<span class="bold">{{vm.title}}</span> Messages

================================================ ===

这仅在起始页上。我想使用 Angular 进行绑定而不是单页。 有任何想法吗。我尝试创建一条返回第一页的路线,但如您所知,它导致了无限循环。我认为 ng-init 是要走的路,但它没有用。 谢谢

dashboard 是一个控制器。这是来自 John Papa 在 Pluralsight 上的 Angular Hotowel 示例。

(function () {
    'use strict';
    var controllerId = 'dashboard';
    angular.module('app').controller(controllerId, ['common', 'datacontext', dashboard]);

    function dashboard(common, datacontext) {
        var getLogFn = common.logger.getLogFn;
        var log = getLogFn(controllerId);

        var vm = this;
        vm.news = {
            title: 'hottowel',
            description: 'Welcome'
        };
        vm.messageCount = 0;
        vm.people = [];
        vm.title = 'Dashboard';

        activate();

        function activate() {
            var promises = [getMessageCount(), getPeople()];
            common.activateController(promises, controllerId)
                .then(function () { log('Activated Dashboard View'); });
        }

        function getMessageCount() {
            return datacontext.getMessageCount().then(function (data) {
                return vm.messageCount = data;
            });
        }

        function getPeople() {
            return datacontext.getPeople().then(function (data) {
                return vm.people = data;
            });
        }
    }
})();

【问题讨论】:

  • 什么是dashboard()?或者vm是什么?
  • 文本已更新为仪表板信息。

标签: angularjs angularjs-ng-init


【解决方案1】:

为了让 yoh 能够调用dashboard(),它应该是控制器作用域上的一个函数(事实并非如此)。
我相信您正在尝试使用controller as 语法(more info):

<div ... ng-controller="dashboard as vm">
    ...
    {{vm.title}}

【讨论】:

  • 感谢专家系统。为了澄清答案,我不需要 ng-init 来加载数据。
    使数据可用。谢谢
  • 没错! (如果你真的喜欢这个答案,也可以点赞 :))
【解决方案2】:

如果你愿意,你不能使用路由器,也不要在你的 Angular 应用程序中设置路由,这样它就不会与你的后端路由器冲突

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签