javascript - AngularJS Controller 不能在嵌套 Controller 中工作

标签 javascript angularjs

有关于选择选项值的问题,我可以在选择选项更改时获取值,但我无法通过搜索按钮获取选择选项的值,它只会给出一个值 未定义。那么问题出在哪里呢?

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);
  }
});

最佳答案

如果你想从一个范围访问一个值,你必须确保它在那个范围内或者在它的祖先范围内。现在您的 ng-model 已在子范围内声明。如果你想从父作用域访问它,你需要在父作用域中声明它。这样,当模型发生变化时,它会在父范围内发生变化,因此可以在两个范围内访问:

工作示例:

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>

失败的例子:

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>

后代作用域可以访问其祖先的值。祖先无法访问其后代的值。

关于javascript - AngularJS Controller 不能在嵌套 Controller 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35567679/

相关文章:

javascript - 访问和更改加载的 KML 文件中的对象(谷歌地球插件)

javascript - 在 React 中重新渲染 DOM

javascript - Angular 引导问题 angular.bootstrap 不适用于我的应用程序

javascript - Angular 工厂成功和错误回调

AngularJs 单元测试内存泄漏

javascript - google drive sdk - 如何通过 JavaScript 上传一个简单的文件

javascript - 将 CSS 表传递到 Stratus 2 Beta 的主题设置中

javascript - 读写 DOCX 文件

angularjs - 如何在 AngularJS 中的两个模块之间共享数据?

angularjs - 如何避免 ionic /Angular 的嵌套调用?