背景图像未显示在AngularJS中
Background image not showing up in AngularJS
我的自定义背景图像没有显示在AngularJS中。不知道为什么。
当我在控制台中测试$scope.setBackground
时,我得到对象{background-image: "url(image.jpg)"}
控制器:
app.controller('backgroundImageCtrl', function($scope, $http) {
$scope.setBackground = {
'background-image' : 'url(image.jpg)'
};
})
HTML:
<script type = "text/ng-template" id="/page.html" ng-controller="backgroundImageCtrl" ng-style="setBackground">
<div>
...
</div>
</script>
var app = angular.module('myApp', []);
app.controller("myCtrl", function ($scope) {
$scope.style= {};
$scope.setBackground = function (date) {
$scope.style = {
'background-image': 'url("https://www.gravatar.com/avatar/10821c595d354140ee66f2a04fb11b7c?s=32&d=identicon&r=PG&f=1")'
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl">
<button ng-click="setBackground()">set background</button>
<div ng-style="style">see the background</div>
</div>
</div>
@Mosh Feu建议的简单解决方案,只需将ng-controller="backgroundImageCtrl"
和ng-style="setBackground"
放置在内部div 中
<script type = "text/ng-template" id="/page.html">
<div ng-controller="backgroundImageCtrl" ng-style="setBackground">
...
</div>
</script>
您应该在使用模板的div中使用setBackground。试试这个。
<div ng-include src="/page.html" ng-style="setBackground"></div>
var mainApp = angular.module("mainApp", ['ngRoute']);
mainApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/addStudent', {
templateUrl: 'addStudent.htm',
controller: 'AddStudentController'
}).
when('/viewStudents', {
templateUrl: 'viewStudents.htm',
controller: 'ViewStudentsController'
}).
otherwise({
redirectTo: '/addStudent'
});
}]);
mainApp.controller('AddStudentController', function($scope) {
$scope.message = "This page will be used to display add student form";
$scope.setBackground = {
'background-image' : 'url(https://www.gravatar.com/avatar/10821c595d354140ee66f2a04fb11b7c?s=32&d=identicon&r=PG&f=1)'
};
});
mainApp.controller('ViewStudentsController', function($scope) {
$scope.message = "This page will be used to display all the students";
$scope.setBackground = {
'background-image' : 'url(https://www.gravatar.com/avatar/6755dcaf172d19cb9976bedcc56cfed3?s=48&d=identicon&r=PG&f=1)'
};
});
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script>
<div ng-app = "mainApp">
<p><a href = "#addStudent">Add Student</a></p>
<p><a href = "#viewStudents">View Students</a></p>
<div ng-view ng-style="setBackground"></div>
<script type="text/ng-template" id="addStudent.htm">
<h2> Add Student </h2>
{{message}}
</script>
<script type = "text/ng-template" id="viewStudents.htm">
<h2> View Students </h2>
{{message}}
</script>
</div>
相关文章:
- Angularjs显示给定ISO格式的日期
- 如何使用AngularJS显示按钮或锚标记值
- 如何使用 Angularjs 显示面包屑
- angularjs显示没有'不起作用
- 从维基百科检索数据并使用angularjs显示
- AngularJS-显示过滤数据的长度
- AngularJS - 显示/隐藏加载器
- Angularjs:显示字符串中的最后四个字符,并将其余字符替换为“X”
- Angularjs 显示来自 json 的日期格式
- AngularJS - 显示/隐藏根范围内的元素
- 使用 angularJs 显示来自套接字 io 的数据
- 使用 Angularjs 显示授权内容
- 如果文本格式化为 html,则使用 angularjs 显示更少/更多的文本
- angularjs显示ng重复列表中的最后5项
- AngularJS显示复杂对象
- 如何使用Wikimedia API和AngularJS显示维基百科页面的缩略图
- Angularjs显示图像斑点
- AngularJS显示JSON数据
- angularJS显示空白页面没有错误-控制器问题
- 为什么angularjs显示标记,而不是谷歌地图中的路线