数据绑定未正确传递到ng-repeat内的控制器功能参数

时间:2014-08-04 05:42:47

标签: javascript angularjs angularjs-ng-repeat angularjs-ng-click

我正在构建一个菜单,允许用户在我的webapp上更改CSS。我的js方法接受一个字符串,然后将其添加到bootswatch CDN的URI中。当我对字符串进行硬编码时代码有效。当我使用数据绑定变量时,它变得有趣。在开发人员工具中,我看到HTML显示应该有数据绑定字符串。当我去查看浏览器尝试获取的URI时,它实际上是将{{style}}添加到URI中。我对这一切都很陌生,所以请原谅任何巨大的缺陷。我愿意接受任何建议。

这是我打开的html标签:

<html data-ng-app="app" lang="en" data-ng-controller="MainCtrl">

这是我的控制器:

app.controller('MainCtrl', function($scope) {
    // default on load for now
    $scope.stylePath = '//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css';
    $scope.styles = ['slate', 'Cyborg', "Darkly", "journal", "Yeti"];
    $scope.setStyle = function(styleName) {
        $scope.stylePath = '//maxcdn.bootstrapcdn.com/bootswatch/3.2.0/' + styleName + '/bootstrap.min.css';
    };
    $scope.changePath = function() {
        $scope.stylePath='//maxcdn.bootstrapcdn.com/bootswatch/3.2.0/slate/bootstrap.min.css';
    };
});

正如您所看到的,在我的数组中,我尝试过不同类型的引号以及小写和大写。 (这个词必须是小写的)还值得注意的是changePath()工作正常。当我尝试传递数据绑定值时,setStyle()从未工作过。传递一个字符串文字没有问题。

以下是相关代码:

<div class="dropdown">
    <a id="ThemeDropdown" class="btn btn-default" role="button" data-toggle="dropdown" title="Choose a theme">Themes <span class="caret"></span></a>
    <ul class="dropdown-menu" role="menu" aria-labelledby="ThemeDropdown">
        <li role="presentation" data-ng-repeat="style in styles">
            <a role="menuitem" tabindex="-1" href="#/settings" ng-click="setStyle('{{style}}')">{{ style }}</a>
        </li>
        <li role="presentation">
            <a role="menuitem" tabindex="-1" href="#/settings" ng-click="changePath()">hardcode slate</a>
        </li>
        <li>
            <a role="menuitem" tabindex="-1" href="#/settings" ng-click="setStyle('darkly')">hardcode darkly</a>
        </li>
     </ul>
</div>

顺便说一句,在setStyle()调用中没有在数据绑定周围加引号会给我一个解析错误。

Error: [$parse:syntax] http://errors.angularjs.org/1.2.21/$parse/syntax?p0=style&p1=is%20unexpected%2C%20expecting%20%5B%3A%5D&p2=12&p3=setStyle(%7B%7Bstyle%7D%7D)&p4=style%7D%7D)
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:6:450
at cb.throwError (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:168:341)
at cb.consume (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:169:266)
at cb.object (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:177:45)
at cb.primary (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:167:478)
at cb.unary (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:174:160)
at cb.multiplicative (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:173:402)
at cb.additive (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:173:262)
at cb.relational (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js:173:126) <a role="menuitem" tabindex="-1" href="#/settings" ng-click="setStyle({{style}})">
angular.js:10023

我可以做我正在做的事情吗?我做错了什么?

1 个答案:

答案 0 :(得分:2)

<a role="menuitem" tabindex="-1" href="#/settings" ng-click="setStyle('{{style}}')">{{ style }}</a>

应该是

<a role="menuitem" tabindex="-1" href="#/settings" ng-click="setStyle(style)">{{ style }}</a>

无需在通话中使用任何绑定。