【问题标题】:Yeoman Mean.js generated custom AngularJS directive not workingYeoman Mean.js 生成的自定义 AngularJS 指令不起作用
【发布时间】:2015-07-31 18:06:27
【问题描述】:

提前感谢您的帮助,我知道这可能是一个简单的问题,但我已经研究了一段时间,无法弄清楚我做错了什么。

上下文信息
我正在尝试为 AngularJS 实现一个简单的自定义指令。我的网页是使用 Mean.js 样板构建的。为了为我的用户模块生成自定义指令,我通过执行此命令使用了 Yeoman 生成器,

yo meanjs:angular-directive compareTo

然后我指定我希望将指令放置在我的用户模块中。这会生成一个带有示例自定义指令的指令目录。这是我的文件夹结构。 http://imgur.com/SZvG706 (我还不能发图片,抱歉)

问题
我的理解是,这应该创建一个带有文本“这是 compareTo 指令”的 h1 属性,并打印一条控制台消息,但我什么也看不到,也没有在控制台中收到任何错误。

守则
这是生成的比较指令文件。我唯一改变的是模板中的 div 到 h1 并添加了控制台 hi 消息。

'use strict';

angular.module('users').directive('compareTo', [
    function() {
        return {
            template: '<h1></h1>',
            restrict: 'E',
            link: function postLink(scope, element, attrs) {
                // Compare to directive logic
                // ...
                console.log('hi');
                element.text('this is the compareTo directive');
            }
        };
    }
]);

这是我的 signup.client.view.html 文件,我尝试在其中使用 compare-to html 标记来实现指令。

<section class="row" data-ng-controller="AuthenticationController">
    <h3 class="col-md-12 text-center">Sign up with you email</h3>
    <compare-to></compare-to>
    <div class="col-xs-offset-2 col-xs-8 col-md-offset-4 col-md-4">
        <form name="userForm" data-ng-submit="signup()" class="signin form-horizontal" novalidate autocomplete="off">
            <fieldset>
                <div class="form-group">
                    <label for="firstName">First Name</label>
                    <input type="text" required id="firstName" name="firstName" class="form-control" data-ng-model="credentials.firstName" placeholder="First Name">
                </div>
                <div class="form-group">
                    <label for="lastName">Last Name</label>
                    <input type="text" id="lastName" name="lastName" class="form-control" data-ng-model="credentials.lastName" placeholder="Last Name">
                </div>
                <div class="form-group">
                    <label for="organization">Organization Name</label>
                    <input type="text" id="organization" name="organization" class="form-control" data-ng-model="credentials.organization" placeholder="Organization Name">
                </div>
                <div class="form-group">
                    <label for="position">Position Title</label>
                    <input type="text" id="position" name="position" class="form-control" data-ng-model="credentials.position" placeholder="Position within organization">
                </div>
                <div class="form-group">
                    <label for="email">Email</label>
                    <input type="email" id="email" name="email" class="form-control" data-ng-model="credentials.email" placeholder="Email">
                </div>
                <div class="form-group">
                    <label for="username">Username</label>
                    <input type="text" id="username" name="username" class="form-control" data-ng-model="credentials.username" placeholder="Username">
                </div>
                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="password" id="password" name="password" class="form-control" data-ng-model="credentials.password" placeholder="Password">
                </div>
                <div class="form-group">
                    <label for="confirmPassword">Confirm Password</label>
                    <input type="password" id="confirmPassword" name="confirmPassword" class="form-control" data-ng-model="confirmPassword" compare-to="credentials.password" placeholder="Confirm Password">
                </div>
                <div class="text-center form-group">
                    <button type="submit" class="btn btn-large btn-primary">Sign up</button>&nbsp; or&nbsp;
                    <a href="/#!/signin" class="show-signup">Sign in</a>
                </div>
                <div data-ng-show="error" class="text-center text-danger">
                    <strong data-ng-bind="error"></strong>
                </div>
            </fieldset>
        </form>
    </div>
</section>

再次感谢!

【问题讨论】:

  • 我对 yeoman mean js 生成器不太熟悉,但听起来你的项目中可能不包含 compare to 指令脚本。这个生成器会编译你的整个应用程序吗?还是使用 AMD 之类的东西?
  • 我只是重复了您所做的,但没有遇到您遇到的问题。对我来说,显示文本并打印日志...

标签: javascript angularjs angularjs-directive yeoman-generator meanjs


【解决方案1】:

所以这个问题真的很傻。我所做的只是关闭我的 Node 服务器实例并重新运行 grunt,我认为我不需要这样做,因为 mean.js 会在您每次进行更改时自动重新编译。但我想有时你需要重新启动它。

【讨论】:

    猜你喜欢
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 2018-02-06
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多