【问题标题】:Angular JS - custome directive is not getting replaced with actual templateAngularjs - 自定义指令没有被实际模板替换
【发布时间】:2014-03-11 14:39:52
【问题描述】:

我正在学习 Angular JS,并想创建我的自定义指令。 我的 javascript 代码没有显示任何错误,但自定义指令没有被我的 HTML 模板替换。 您能否指导如何调试此问题或此代码有什么问题? jsFiddle link

<body ng-controller="customdirectivecontroller">
    <p>Placed custom directive here!</p>
    <mytextbox></mytextbox>
</body>

------------------
var customdirectiveapp = angular.module('customdirectiveapp', []);

customdirectiveapp.controller = ('customdirectivecontroller', function ($scope, $http) {
    $scope.name = "xxx";
});

customdirectiveapp.directive = ('mytextbox', function () {
    var directive = {};

    directive.restrict = 'E'; /* restrict this directive to elements */

    directive.template = "My first directive: ";

    return directive;
});

【问题讨论】:

    标签: angularjs custom-controls


    【解决方案1】:

    您的代码中有几个错误

    1) 你的变量名在指令定义中是错误的,它是 customdirectveapp 但它应该是 customdirectiveapp

    2)你的指令定义是错误的,你应该定义控制器,指令......像这样

    customdirectiveapp.controller('customdirectivecontroller', function ($scope, $http) {
        ....
    });
    

    而不是这个

    customdirectiveapp.controller = ('customdirectivecontroller', function ($scope, $http) {
        ....
    });
    

    您的应用程序的 PLUNKER 正在运行

    【讨论】:

    • @PST 不客气...是不是您的问题的答案?我是新来的,不明白人们如何接受他们的问题的答案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    相关资源
    最近更新 更多