$ scope undefined in modal angularjs

时间:2013-11-16 21:08:57

标签: javascript angularjs angular-ui-bootstrap

我在角度中使用模态,但是当模态打开时,这里的值不与模型绑定我不知道为什么如果有任何错误,如果有一些错误,则必须理解这种情况。

modal.html

<script type="text/ng-template" id="categoryModal.html">

    <form class="form-horizontal" name="category_form" novalidate>
        <div class="modal-header">
            <a class="close" ng-click='cancel()'><i class="icon-remove-circle icon-bold"></i> </a>
            <h3>Category</h3>
        </div>

        <div class="modal-body">

            <div class="form-group">
                <label for="category_Name" class="col-lg-3 form-label">Category Name:</label>
                <div class="col-lg-8">
                    <input class="form-control" id="category_Name" ng-model="category.name" name="category_Name" placeholder="Category Name" required/>
                    <div class="error" ng-show="category_form.category_Name.$dirty && category_form.category_Name.$invalid">
                        <small class="error errorFields" ng-show="category_form.category_Name.$error.required">
                            Category Name is required.
                        </small>

                    </div>
                </div>
            </div>

        </div>

        <div class="modal-footer">
            <button  ng-click='saveCategory()' class="btn btn-primary" ng-disabled="category_form.$invalid">
                <i class="icon-ok-sign icon-white"></i> Add
            </button>

            <button ng-click='cancel()' class="btn btn-warning">
                <i class="icon-remove-circle icon-white"></i> Cancel
            </button>
        </div>

    </form>
</script>

modalController.js

app.controller('brandModalCtrl', function ($rootScope, $scope, $modalInstance) {

    // Save Brand

    $scope.saveCategory = function () {
       console.log($scope.category) // undefined
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

});

1 个答案:

答案 0 :(得分:0)

我会尝试将相同的$ scope添加到modalOptions,并让模态共享控制器的$ scope变量。两者都放在$ rootScope上太激烈了。理想情况下,你的模态应该有一个隔离范围,但听起来你需要一些重叠。