Angular JS - custome directive is not getting replaced with actual template

只愿长相守 提交于 2019-12-12 04:44:24

问题


I am learning Angular JS and would like to create my custom directive. My javascript code is not showing any error but the custom directive is not getting replaced with my HTML template. Can you please guide on how to debug this issue or what is wrong with this code? 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;
});

回答1:


There are several errors in your code

1) your variable name is wrong at directive defination it was customdirectveapp but it should be customdirectiveapp

2) your directive definations are wrong you should define controller,directive... like this

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

instead of this

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

and here is working PLUNKER of your application



来源:https://stackoverflow.com/questions/22317650/angular-js-custome-directive-is-not-getting-replaced-with-actual-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!