AngularJS Controller not working in nested Controller

Joseph Goh

Got a questions regarding the select option value, i can get the value as the select options changed, but i am not able to get the value of the select options thru the search button, it will just give a value undefined. So where is the problem?

index.html

<div ng-controller="MatIncListCtrl">
  <button class="fluid labeled icon blue ui button top attached"><i class="ellipsis vertical icon"></i>Toggle Filters Pane</button>
  <div class="ui attached segment" uib-collapse="isCollapsed">
    <form role="form" class="ui form">
      <div class="fields">
        <div class="twelve wide field" ng-controller="DistinctSupplierCtrl">
          <label>Supplier</label>
          <select ng-model="Supplier" class="ui fluid dropdown" ng-options="x.supp_no as x.supp for x in data" ng-change="selectAction()">
            <option></option>
          </select>
        </div>
      </div>
    </form>
    <br />
    <button type="button" class="ui orange fluid labeled icon button" tabindex="0" ng-click="FindMatInc()"><i class="search icon"></i>Search</button>
  </div>
</div>

controller.js

.controller('DistinctSupplierCtrl', function($scope, $http) {
  $scope.selectAction = function() {
    console.log($scope.Supplier);
  };
  var xhr = $http({
    method: 'post',
    url: 'http://localhost/api/list-distinct-supp.php'
  });
  xhr.success(function(data){
    $scope.data = data.data;
  });
})

.controller('MatIncListCtrl', function ($scope, $http) {
  $scope.FindMatInc = function (){
    console.log($scope.Supplier);
  }
});
iH8

If you want to access a value from a scope, you've got to make sure it's in that scope or in the scope of it's ancestors. Now your ng-model is declared in the child scope. If you want to access it from the parent scope, you'll need to declare it in the parent scope. That way when the model get changed, it changed in the parent scope and thus accessible in both scopes:

Working example:

angular.module('App', []);

angular.module('App').controller('ControllerParent', [
             '$scope',
    function ($scope) {
        // Model value declared in parent scope
        $scope.selected = {};
    }
]);

angular.module('App').controller('ControllerChild', [
             '$scope',
    function ($scope) {
        $scope.options = [{
            id: 1,
            name: 'Alpha'
        }, {
            id: 2,
            name: 'Bravo'
        }, {
            id: 3,
            name: 'Charlie'
        }, {
            id: 4,
            name: 'Delta'
        }];
    }
]);
<!DOCTYPE html>
<html ng-app="App">
    <head>
        <script type="application/javascript" src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.js"></script>
    </head>
    <body ng-controller="ControllerParent">
        <div ng-controller="ControllerChild">
            <select ng-model="selected.value" ng-options="option.name for option in options"></select>
            ControllerSub: {{selected}}
        </div>
        ControllerMain: {{selected}}
    </body>
</html>

Failing example:

angular.module('App', []);

angular.module('App').controller('ControllerParent', [
             '$scope',
    function ($scope) {}
]);

angular.module('App').controller('ControllerChild', [
             '$scope',
    function ($scope) {
        // Model value declared in child scope
        $scope.selected = {};
        $scope.options = [{
            id: 1,
            name: 'Alpha'
        }, {
            id: 2,
            name: 'Bravo'
        }, {
            id: 3,
            name: 'Charlie'
        }, {
            id: 4,
            name: 'Delta'
        }];
    }
]);
<!DOCTYPE html>
<html ng-app="App">
    <head>
        <script type="application/javascript" src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.js"></script>
    </head>
    <body ng-controller="ControllerParent">
        <div ng-controller="ControllerChild">
            <select ng-model="selected.value" ng-options="option.name for option in options"></select>
            ControllerSub: {{selected}}
        </div>
        ControllerMain: {{selected}}
    </body>
</html>

Descendant scopes have access to the values of their ancestors. Ancestors don't have access to values of their descendants.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Controller in Nested views of AngularJS

From Dev

Controller in Nested views of AngularJS

From Dev

binding not working in nested controller

From Dev

AngularJS greetController Controller not working

From Dev

angularjs 1.3 controller as not working

From Dev

multiple controller not working in angularjs

From Dev

AngularJS controller not working

From Dev

angularjs 1.3 controller as not working

From Dev

AngularJS greetController Controller not working

From Dev

AngularJS - Toaster not working in Controller

From Dev

AngularJs multiple instances and nested controller

From Dev

Angularjs - data binding not working with controller

From Dev

AngularJS ng-controller not working

From Dev

Angularjs $setPristine not working with controller as syntax

From Dev

Show and Hide Controller not working, AngularJS

From Dev

Simple AngularJS Controller Demo Not Working

From Dev

AngularJS: Initialize nested scope variables in controller

From Dev

How do I initialize a nested controller in AngularJS?

From Dev

Use ng-model in nested Angularjs controller

From Dev

angularjs nested controller does not receive injected service

From Dev

Use ng-model in nested Angularjs controller

From Dev

Automatically instantiate AngularJS controller nested in templateUrl

From Dev

A controller into a controller in AngularJS

From Dev

AngularJS ng-click not working with configured Controller

From Dev

AngularJS beginner: ng-controller not working

From Dev

AngularJS ng-controller not working after bootstraping

From Dev

JS function defined in AngularJS controller not working

From Dev

AngularJS stops working as soon as I include a controller

From Dev

Angularjs controller not working, throws error (newbie)

Related Related

  1. 1

    Controller in Nested views of AngularJS

  2. 2

    Controller in Nested views of AngularJS

  3. 3

    binding not working in nested controller

  4. 4

    AngularJS greetController Controller not working

  5. 5

    angularjs 1.3 controller as not working

  6. 6

    multiple controller not working in angularjs

  7. 7

    AngularJS controller not working

  8. 8

    angularjs 1.3 controller as not working

  9. 9

    AngularJS greetController Controller not working

  10. 10

    AngularJS - Toaster not working in Controller

  11. 11

    AngularJs multiple instances and nested controller

  12. 12

    Angularjs - data binding not working with controller

  13. 13

    AngularJS ng-controller not working

  14. 14

    Angularjs $setPristine not working with controller as syntax

  15. 15

    Show and Hide Controller not working, AngularJS

  16. 16

    Simple AngularJS Controller Demo Not Working

  17. 17

    AngularJS: Initialize nested scope variables in controller

  18. 18

    How do I initialize a nested controller in AngularJS?

  19. 19

    Use ng-model in nested Angularjs controller

  20. 20

    angularjs nested controller does not receive injected service

  21. 21

    Use ng-model in nested Angularjs controller

  22. 22

    Automatically instantiate AngularJS controller nested in templateUrl

  23. 23

    A controller into a controller in AngularJS

  24. 24

    AngularJS ng-click not working with configured Controller

  25. 25

    AngularJS beginner: ng-controller not working

  26. 26

    AngularJS ng-controller not working after bootstraping

  27. 27

    JS function defined in AngularJS controller not working

  28. 28

    AngularJS stops working as soon as I include a controller

  29. 29

    Angularjs controller not working, throws error (newbie)

HotTag

Archive