简体   繁体   English

AngularJS Ng样式

[英]AngularJS Ng-style

I am trying to use ng-style for a div. 我正在尝试为div使用ng-style。 I tried all sorts of things and checked about it on Google and Stackoverflow but I can't get it to work. 我尝试了各种方法,并在Google和Stackoverflow上进行了检查,但无法正常工作。 Only testInsideDiv text gets displayed. 仅显示testInsideDiv文本。 I check if I mised any ' but I don't see it and I have tried a lot of diffrent things. 我检查是否错了任何内容'但我看不到,并且尝试了很多不同的事情。 Anyone sees what the problem is? 有人看到问题出在哪里吗?

Here is the code: 这是代码:

<div ng-style="{'width' : {{deviceWidth}}, 'height' : {{deviceHeight}}, 'background-image':{{imagePath}}, 'background-size' : {{deviceWidth}} {{deviceHeight}}, 'background-repeat':'no-repeat'" ng-click="addOnClick($event)">testInsideDiv
                </div>

And it looks like this in debuger: 在调试器中看起来像这样:

 <div ng-style="{'width' : 569px, 'height' : 300px,
 'background-image':'url('http://scada.voco.si/Portals/0/Scadas/2/28/28.jpg')',
 'background-size' : 569px 300px}, 'background-repeat':'no-repeat'"
 ng-click="addOnClick($event)"
 class="disable-user-behavior">testInsideDiv
                 </div>

您不得使用双括号:

<div ng-style="{'width': deviceWidth, 'height': deviceHeight}">...</div>

Main error - you forgot the closing curly brace of the ng-style object . 主要错误- 您忘记了ng样式对象的右花括号

You use camelCase if it's easier for you, and if you're interpolating variables, make sure you quote them. 如果您更方便,可以使用camelCase;如果要对变量进行插值,请确保引用它们。 However, you probably don't want interpolation, but binding, so by changing scope variables, the style will also change: 但是,您可能不希望插值,而是绑定,因此通过更改范围变量,样式也会更改:

<div ng-style="{
    width : deviceWidth, 
    height : deviceHeight, 
    backgroundImage: imagePath, 
    backgroundSize: deviceWidth + ' ' + deviceHeight, 
    backgroundRepeat: 'no-repeat'
    }" ng-click="addOnClick($event)">testInsideDiv</div>

See it in action here: 在这里查看实际操作:

 angular.module('app', []) .controller('Ctrl', function($scope) { $scope.deviceWidth = '460px'; $scope.deviceHeight = '276px'; $scope.imagePath = 'url(http://cdn2.business2community.com/wp-content/uploads/2014/12/Super-Mario-no-longer-the-007.jpg)'; // changing scope properties will make the style change // because the values are not interpolated // as in your original code $scope.shrink = function() { $scope.deviceWidth = '230px'; $scope.deviceHeight = '138px'; } }) 
 div { border: 1px solid black; transition: all .5s linear; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="Ctrl" ng-style="{ width : deviceWidth, height : deviceHeight, backgroundImage: imagePath, backgroundSize: deviceWidth + ' ' + deviceHeight, backgroundRepeat: 'no-repeat' }" ng-click="shrink()">testInsideDiv Click to shrink</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM