AngularJS:从childController获取值到parentController

时间:2016-06-15 00:02:10

标签: angularjs forms

我无法在我的表单中获取值,我有2个控制器,我正在尝试打印第二个控制器(CtrlB)内的下拉列表的值。如何在父控制器上获取此子控制器的值?一切都工作得很好,渲染与我的下拉列表,我的表单,我的验证的值,除了获得$ scope.dropdownlist的值。有什么帮助吗?

我的CtrlA控制器

$scope.register = function (isValid) {

    name = $scope.name; // works!
    documentType = $scope.dropdownlist; // Got undefined :(
}

我的指示

<select ng-model="dropdownlist" <!-- I can not get this value inside my CtrlA controller-->
        ng-options="x.description for x in types">
</select>

我的表单     

    <form role="form"
            name="nonClientRegisterForm"
            ng-submit="register(nonClientRegisterForm.$valid);"
            novalidate>

            <div class="form-group" 
                ng-class="{ 'has-error' : nonClientRegisterForm.name.$invalid && !nonClientRegisterForm.name.$pristine }">

                    <input 
                        type="text"
                        name="firstinput"
                        ng-model="firstinput" 
                        class="form-control"
                        required />
            </div>

            <div>
                <!-- This component has its own controller that fills this dropdownlist -->
                <dropdownlist ng-controller="CtrlB"></dropdownlist>
            </div>

            <span ng-show="
                (nonClientRegisterForm.name.$invalid && 
                !nonClientRegisterForm.name.$pristine)"
class="help-block">
                Debes completar todos los campos.
            </span>

            <button 
                type="submit" 
                ng-disabled="nonClientRegisterForm.$invalid">
                continuar
            </button>                        
        </div>                    
    </form>
</div>

1 个答案:

答案 0 :(得分:0)

你应该使用$ rootscope从子控制器获取值到父控制器。

$scope.register = function (isValid) {
name = $scope.name; 
documentType = $rootscope.dropdownlist;}

  function CtrlB($scope) {
$rootscope.dropdownlist="A";}