Angularjs - 数据绑定无法与控制器一起使用

时间:2014-10-07 16:07:18

标签: javascript angularjs twitter-bootstrap data-binding

我正在开发一个使用bootstrap(v3.2.0)和angularjs(v1.2.26)的应用,我想创建一组columns,可以通过{ {3}}

我的下拉数据绑定没问题,但是当用户在下拉列表中选择不同数量的列时,我无法更改引导响应列类。

到目前为止,此dropdown显示了我的代码。

html ...

<div data-ng-app="">
    <div class="container-fluid">
        <nav class="navbar navbar-default" role="navigation">
            <div class="container-fluid">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">   <span class="sr-only">Toggle navigation</span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>

                    </button>   <span class="navbar-brand navbar-collapse-header">Header</span>

                </div>
                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav navbar-right">
                        <li data-ng-controller="ColumnController" id="columnDropDown" class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Columns: {{columnLayout.value}} <span class="caret"></span></a>

                            <ul class="dropdown-menu" role="menu">
                                <li data-ng-repeat="layout in columnLayouts">   <a data-ng-click="changeLayout(event)" href="#" class="portlet-column-number">{{layout.text}}</a>

                                </li>
                            </ul>
                        </li>
                    </ul>
                </div>
                <!-- /.navbar-collapse -->
            </div>
            <!-- /.container-fluid -->
        </nav>
    </div>
    <div class="row">
        <div data-ng-controller="ColumnController" data-ng-class="getLayoutClass()">
            <div class="panel panel-default">
                <div class="panel-heading mo-product-header">Header 1</div>
                <div class="panel-body">More content</div>
                <div class="panel-footer">Layout class is {{getLayoutClass()}}</div>
            </div>
        </div>
        <div data-ng-controller="ColumnController" data-ng-class="getLayoutClass()">
            <div class="panel panel-default">
                <div class="panel-heading mo-product-header">Header 2</div>
                <div class="panel-body">Some other content</div>
                <div class="panel-footer">Layout class is {{getLayoutClass()}}</div>
            </div>
        </div>
        <div data-ng-controller="ColumnController" data-ng-class="getLayoutClass()">
            <div class="panel panel-default">
                <div class="panel-heading mo-product-header">Header 3</div>
                <div class="panel-body">Some content</div>
                <div class="panel-footer">Layout class is {{getLayoutClass()}}</div>
            </div>
        </div>
    </div>
</div>

和js ......

function ColumnController($scope) {
    $scope.columnLayouts = [{
        value: 3,
        text: "Reset",
        layoutClass: "col-sm-6 col-md-4"
    }, {
        value: 1,
        text: "1",
        layoutClass: "col-sm-12"
    }, {
        value: 2,
        text: "2",
        layoutClass: "col-sm-6"
    }, {
        value: 3,
        text: "3",
        layoutClass: "col-sm-4"
    }];
    $scope.changeLayout = function () {
        $scope.columnLayout = this.layout;
    }
    $scope.getLayoutClass = function(){
        return $scope.columnLayout.layoutClass;
    }
    $scope.columnLayout = $scope.columnLayouts[0];
}

更新:

好的,事实证明我应该只添加&#39; ColumnController&#39;到一个元素,在这种情况下,该元素最适合作为所有其他元素的包装。

但是,我认为为了进一步改进这一点,我可能会创建一个单独的导航控制器,然后创建有关更改的fiddle消息,在这种情况下,我可以让ColumnController侦听有关列更改的消息向他们致意,改变驱动列布局的模型。

1 个答案:

答案 0 :(得分:2)

问题是你放了太多data-ng-controller="ColumnController"。您无需将其添加到使用角度绑定{{}}ngRepeat的每个标记。包装容器上只需要一个。例如:

<div data-ng-app="" data-ng-controller="ColumnController">
    ...
</div>

演示:http://jsfiddle.net/pykm0tkv/25/