Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I'm trying to use angularjs to create a page that does the following:

  1. Is initially empty, save for a dropdownlist that is automatically populated with apps.
  2. upon selecting one of those apps, data about it will be called from another controller to the page.

I was successfully able to get the dropdownlist to automatically populate. however, I'm having issues getting it to make the page with ng-change, which I thing is due to the nested ng-controllers. The chartappsuccessfullogins function is not being called at all in my browser. Can anyone help me? Code is below:

My main html page. Note the use of ng-init:

    <div ng-controller="chartsuccessfulapploginsController">
        <div ng-controller="allappsController" ng-init="add()">
            <form name="myForm">
                <label for="repeatSelect"> Repeat select: </label>
                <select name="repeatSelect" id="repeatSelect" ng-model="data.repeatSelect" ng-change="chartsuccessfulapploginsController.add(value)">
            <option ng-repeat="option in data.availableOptions" ng-init="Index = $index"  value="{{data.availableOptions[Index].id}}" ng-model="APPID" >{{data.availableOptions[Index].name}}</option>
        </select>
            </form>
            <hr>
            <p> {{data}}</p>
            <p> {{data.id[0]}}</p>
            <p> {{data.name[0]}}</p>

            <tt>repeatSelect = {{data.repeatSelect}}</tt><br/>
        </div>
        <p>{{returnCount}}</p>
        <table border = "1">
            <tr>
                <td>{{chartObject.data}}</td>
                <td>{{returnCount}}</td>

            </tr>
        </table>
        <div google-chart chart="chartObject" style="height:600px; width:100%;"></div>
    </div>

My get all apps controller. The html page above relies on this to populate the dropdownlist.

angular.module('scotchApp').controller('allappsController',['$scope', function($scope){
    var userurl='';
    $scope.add = function(){

        userurl = 'http://localhost:8085/rest/uafapp/appslist';
        var userdata = {};
        var userconfig =
        {
            headers: {
                'Content-Type':'application/json'
            }
        };
        var userPostRequest = $.get(userurl, userdata, userconfig);
        var userjson = '{\"USER_DATA_RETRIEVED\" : \"fail\"}';
        userPostRequest.done(function(userdata){

            userjson = JSON.stringify(userdata);
            console.log("userjson :: " + userjson);

            var postResponse = jQuery.parseJSON(userjson);
            $scope.returnName = postResponse['apps'];
            var availableOptionsArray = [];

            for(i = 0; i < postResponse['apps'].length; i++){
                var availableOptions = {};
                availableOptions['id'] = postResponse['apps'][i]['appId'];
                availableOptions['name'] = postResponse['apps'][i]['appName'];
                availableOptionsArray[i] = availableOptions; 
            }
            var returnData = {};
            returnData['repeatSelect'] = null;
            returnData['availableOptions'] = availableOptionsArray;

            $scope.data = returnData;
            console.log($scope.returnData);
            $scope.$apply()
        });

    };
}]);

Part of the controller that defines the chart. It's pretty long, so I didn't include the irrelevant code.

   angular.module('scotchApp').controller('chartsuccessfulapploginsController',['$scope','$route','$http','AuthTokenService', function($scope, $route, $http, AuthTokenService){
        var appurl = '';
        var failedappurl= '';

        $scope.add = function(APPID) {

...}
share|improve this question
    
The problem is you're not using controller aliases ex. ng-controller="allappsController as allapps". and your init should then be ` ng-init="allapps.add()"` – Ervald Jun 22 at 19:30

Is your allappsController within your chartsuccessfulapploginsController in your controller file?

It should be inside because allappsController is the child scope, and chartsuccessfulapploginsController is the parent scope. You are trying to access the parent scope from the child scope.

If it is not inside, it thinks that ng-change="chartsuccessfulapploginsController.add(value)" is a new controller.

If that is the issue, the fix would be something like this:

    angular.module('scotchApp').controller('chartsuccessfulapploginsController',['$scope','$route','$http','AuthTokenService', function($scope, $route, $http, AuthTokenService){
            var appurl = '';
            var failedappurl= '';

            $scope.add = function(APPID) {} ...


            //allappsController inside chartsuccessfulapploginsController
            angular.module('scotchApp').controller('allappsController',['$scope',function($scope){
            var userurl='';
            $scope.add = function(){ ... };

           }]);
}]);

Check this out: Use ng-model in nested Angularjs controller

share|improve this answer
    
what would the html look like? where would the ng-model go? – James Falter Jun 22 at 19:36
    
You are not using the ng-model, the question is similar in the fact that you are both trying to access a outer controller from an inner controller. Your HTML is fine, since your ng-change using hartsuccessfulapploginsController is nested within your inner controller (allappsController). Let me know if I misunderstand your question in your comment. – robert.otherone Jun 22 at 19:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.