在angularJS中使用ng-style而不是style时出错

时间:2015-09-26 07:52:23

标签: javascript html css angularjs

这是我显示类别的代码:

    <select ng-style="margin-left:30px;" ng-show="ShowBrand" ng-model="SelectedBrand" ng-options="x as x.name for x in Brands | orderBy:'name'" ng-class='form-control'>
        <option value="" ng-selected="selected">Select a Brand</option>
    </select>

我在colsole窗口中看到以下错误:

    Error: [$parse:syntax] http://errors.angularjs.org/1.2.16/$parse/syntax?p0=%3A&p1=is%20an%20unexpected%20token&p2=12&p3=margin-left%3A30px%3B&p4=%3A30px%3B
        at Error (native)
        at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:6:450
        at $a.throwError (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:165:141)
        at $a.parse (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:164:6)
        at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:96:122
        at k (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:102:227)
        at h.$watch (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:103:380)
        at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:197:313
        at J (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:53:345)
        at f http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:46:399

select ng-style="margin-left:30px;" ng-show="ShowBrand" ng-odel="SelectedBrand" ng-options="x as x.name for x in Brands | orderBy:'name'" ng-class="form-control" class="ng-pristine ng-valid">

此错误仅在我使用ng-style时。如果我使用样式而不是ng-style,则无法在IE9上呈现。

此错误的原因是什么以及如何解决?

1 个答案:

答案 0 :(得分:7)

ngStyle期望有效的表达式表示法。所以尝试传递对象:

ng-style="{'margin-left': '30px'}"