,
Angular dom , . - .
, , jQuery, , dom.
, - InformerController, - , .
app.controller('InformersController', function($scope, InformerService) {
$scope.informs = InformerService.get();
$scope.close = function (index) {
InformerService.close(index)
}
});
:
<div ng-controller="InformersController">
<div ng-repeat="inform in informs">
<div class="alert {{inform.alertClass}} fade in informer">
<button type="button" ng-click="close($index)" class="close">×</button>
<div class="valignCenterWrapper">
<div class="valignCenter">
{{inform.message}}
</div>
</div>
</div>
</div>
</div>
- , , InformerService , .
app.service('InformerService', function () {
var informs = [];
this.get = function () {
return informs;
};
this.inform = function (message, type) {
informs.push({
alertClass: 'alert-' + type,
message: message
});
}
this.close = function (index) {
informs.splice(index, 1);
}
});
:
app.controller('SomeController', function($scope, InformerService) {
$scope.doError = function (msg, type) {
InformerService.inform(msg, type);
};
});
:
<div class="well" ng-controller="SomeController">
<button class="btn btn-danger" ng-click="doError('Hello', 'error')">Error</button>
<button class="btn btn-info" ng-click="doError('Hello', 'info')">Info</button>
</div>
: http://jsfiddle.net/zc3YH/6/