locked
hwo to add controller with empty ng-app vlaue RRS feed

  • Question

  • User81789783 posted

    i have below code

    <html>
    <head>
    <title>AngularJS First Application</title>
    </head>
    
    <body>
    <h1>My First Application</h1>
    
    <div ng-app = "">
    <p>Enter your Name: <input type = "text" ng-model = "name"></p>
    <p>Welcome <span ng-bind = "name"></span>!!</p>
    </div>
    
    <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    
    </body>
    </html>

    now i wanted to add any helloworld controller ,provide that ng-app have blank value how can do that ??

    Wednesday, October 23, 2019 3:51 AM

All replies

  • User-719153870 posted

    Hi erum,

    now i wanted to add any helloworld controller ,provide that ng-app have blank value how can do that ??

    The code you provided is working well, so i guess your requirement is actually about the ealier thread you posted, right?

    If so, you can achieve this goal with the $controllerProvider.allowGlobals() to set the controller be used global.

    You can refer to Using ng-app without a value, in this thread, it's mentioned that you can use the global controller before Angular 1.3, but in 1.6.4, the $controllerProvider.allowGlobals() is needed.

    Also please check AngularJS, allowGlobals for global controllers about how to use this statement.

    In your case, you can update your code like this:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <meta charset="utf-8" />
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
        <script>
            angular.module("ng").config(function ($controllerProvider) {
                $controllerProvider.allowGlobals();
            });
            function FunctionHelloworld($scope) {
                $scope.message = "ERum";
            }
        </script>
    </head>
    <body>
        <div ng-app ng-controller="FunctionHelloworld">
            Hello:
            {{message}}
        </div>
    </body>
    </html>

    However, this $controllerProvider.allowGlobals() is generally not recommended because of namespace related risk.

    Best Regard,

    Yang Shen

    Wednesday, October 23, 2019 5:39 AM