0

I am working on SPA and I include HTML pages into one basic page by ng-include.Let's call this basic page as external and the included pages as internal.In an internal page I have set an input attribute to be ng-model="userd.Username" and in the external page i would like to get the value of this input.

In some internal page register.html there is the following code:

<div class="form-group">
        <div class="form-group">
            <span class="regForm text-danger">*</span> <input
                ng-model="userd.Username" type="text" maxlength="10"
                placeholder="Enter your name" class="form-control input-md"
                required />
        </div>
        <div class="form-group">
            <span class="regForm text-danger">*</span> <input
                ng-model="userd.Password" type="password" maxlength="8"
                placeholder="Enter your password" class="form-control input-md"
                required />
        </div>
        <div class="form-group">
            <span class="regForm text-danger">*</span><input
                ng-model="userd.Nickname" type="text" maxlength="20"
                placeholder="Enter your nickname" class="form-control input-md"
                required />
        </div>
        <div class="form-group">
            <input ng-model="userd.Description" type="text" maxlength="50"
                placeholder="Enter description about you"
                class="form-control input-md" />
        </div>
        <div class="form-group">
            <input ng-model="userd.Photo" type="text"
                placeholder="Enter photo URL" class="form-control input-md" />
        </div>
        <div class="form-group">
            <button ng-click="registerBtn()" class="btn btn-primary">Register</button>
        </div>
    </div>

In the external page:

    <div ng-include="'register.html'" ng-controller="registerCon" ng-show="logreg"></div>

I've tried to use the $rootscope as following in the main controller of the external page:

var appvar = angular.module('myApp', []);
    //The Main Controller of the page (single web page)
    appvar.controller('myCtrl',['$scope','$http','$rootScope',function($scope, $http,$rootScope) {                      
                $rootScope.storedsession="";
    }]);

And in the controller 'registerCon' I wrote:

appvar.controller('registerCon',['$scope','$http','$rootScope',function($scope, $http, $rootScope) {
                $scope.registerBtn = function() {
                    console.log(this.userd);
                    $http.post("http://localhost:8080/ExampleServletv3/registeruser",this.userd)
                            .success(function(response) {
                                console.log(response);
                                setTimeout(function () {
                                    $scope.$apply(function(){
                                        $rootScope.storedsession=this.userd.Username;
                                        console.log($rootScope.storedsession);
                                    });
                                });
                            });
                };
    }]);

but it prints undefined for console.log($rootScope.storedsession); in the console,I also tried $scope instead of $rootScope but it didn't work too.

Can someone help me please?

Thanks

user6561572
  • 219
  • 3
  • 14
  • Your code does not include anything why `console.log(this.userd);` should log something else than `undefined` – devqon Feb 07 '17 at 11:50
  • @devqon this is only a little part of the code ,only what i need in order to explain my problem,any way i will include the rest – user6561572 Feb 07 '17 at 11:54
  • @devqon also `console.log(this.userd);` works and it prints the details the user entered trying to register,but the problem is that i can't get a specific key value of the object `userd` – user6561572 Feb 07 '17 at 12:04
  • Declare the object `userd` in your controller. Then try to change the values using view. It will work – Arumuga Raja Feb 07 '17 at 12:34
  • @Arumuga Raja what do you mean by "..using view",is this an attribute? I'm not finding it,could you please write a simple example? – user6561572 Feb 07 '17 at 12:43
  • @shery create one object like `$scope.userd = {};` in your controller while initializing. Then once the value changed in the view it will return the value instead of `undefined` – Arumuga Raja Feb 07 '17 at 12:47
  • @Arumuga Raja thank you so so much ,it really worked ,in addition I changed the row ` $rootScope.storedsession=this.userd.Username;` to be ` $rootScope.storedsession=$scope.userd.Username;` ,thanks;) – user6561572 Feb 07 '17 at 12:57

0 Answers0