我刚刚开始与Angular(再次)。Angular JS数据绑定
我找两个相关问题,就以下plunker
http://plnkr.co/edit/anfQW9NcZs1WuPL8yKdJ
var app = angular.module('Designer', [])
.factory('TextModel', function() {
return {
'textAlign': 'left',
'fontFamily': 'Arial'
};
});
app.controller('TextController', function($scope, TextModel){
$scope.master = TextModel;
$scope.$watch("master",function(){
$scope.textStyles = {
'text-align': $scope.master.textAlign,
'font-family': $scope.master.fontFamily
};
}, true);
$scope.setAlignment = function(newAlignment){
$scope.master.textAlign = newAlignment;
};
$scope.setFont = function(font){
$scope.master.fontFamily = font;
};
});
在这种回答,我怎么能应用样式仅当前焦点输入框,而不是两个。
同样,如果我然后更改一个字体为Times和另一个为Verdana,当我单击时,如何将选择框更新为选定/聚焦的输入框的字体。
感谢
虽然这回答了这个问题。如果有100个文字区域,它很快就会变得混乱。在这种情况下,有没有更好的方式来动态设置当前集中的textarea上的样式,而不是拥有。 text1styles text2styles ... text100styles – 2014-10-07 21:16:12
我同意这种方法并不是最佳的,即使它在您的蹲跳者的情况下工作。更好的解决方案是使用pub/sub模式。在Angular中,你需要编写一个指令来监听字体变化事件,以便它可以更新元素的样式,如果它具有焦点。 – 2014-10-07 23:58:20