简体   繁体   中英

angular js custom directive not working without the ng-app directive

I have created a custom directive named myAddress. I shall be using it in another html page. The custom directive is only working if I give it a ng-app in the element as below.

<my-Address  ng-app="customModule">  </my-Address>.

but not working if I write this

<my-Address >  </my-Address>.

I should get it working without the ng-app in the element.

The following is the custom directive code.

var mainApp = angular.module("customModule", ['ngSanitize', 'schemaForm']);
    mainApp.directive('myAddress', [function() {
        var directive ={};
        directive.restrict = 'E';
        directive.templateUrl = 'customDirective.html';
        directive.controller = 'myController';
        //directive.module = 'customModule';
        directive.compile = function($scope, element, attributes) {
        }
        return directive;
    }]);

The html where the directive is added.

<!Doctype html>
<html >

<head>
    <title>Angular JS Custom Directive</title>

    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="style.css" />
</head>

<body>
<h2> some heading </h2>

    <my-Address  ng-app="customModule">  </my-Address>

<link rel="stylesheet"
      href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />

<script
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script
        src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script
        src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script src="customDirectiveScript.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-sanitize.js"></script>
<script src="tv4.js"></script>
<script src="ObjectPath.js"></script>
<script src="schema-form.js"></script>
<script src="bootstrap-decorator.js"></script>


</body>
</html>

Please help, thanks in advance.

try the following

<!Doctype html>
<html >

<head>
    <title>Angular JS Custom Directive</title>

    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
     <link rel="stylesheet" href="style.css" />
</head>

<body ng-app="customModule">
<h2>NationWide Standardized </h2>

    <my-Address  >  </my-Address>

<link rel="stylesheet"
  href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />

<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script
    src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<script src="customDirectiveScript.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-sanitize.js"></script>
<script src="tv4.js"></script>
<script src="ObjectPath.js"></script>
<script src="schema-form.js"></script>
<script src="bootstrap-decorator.js"></script>


</body>
</html>

Take a good look at the ng-app on the body tag.

If you want to use it inside another ng application. you need to inject you customModule inside this application

see this example

angular.module('myApp',['customModule'])

the custom module you wrote will be injected inside another angular application which gives you access to you my-address directive

Your <html> needs a ng-app attribute, if you're not going to call angular.bootstrap() yourself (you might want to do that for reasons related to asynchrony).

You want to put that on your top-level tag (which should be html ), because anything outside of that won't be picked up by angular (that means controllers, views, foreaches, etc, etc).

This is why your code doesn't work: Angular is not "enabled" outside of the ng-app -enabled element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM