{{}}和角度中的ng-bind之间是否有任何区别.
我对Angular很新.我开始使用{{}},然后在文档中找到ng-bind.我认为他们做同样的工作,但为什么一个额外的指令,如果没有那么请告诉差异.
有人可以解释为什么按下按钮时没有更新此插件中ng-model的输入?
http://plnkr.co/edit/8JpyUVM8dY8aoV4fi5hC?p=preview
HTML
<body ng-controller="MainCtrl">
Using ng-model: <input type="text" ng-model="myDate"><br/>
Using expression: {{ myDate }}<br/>
<button ng-click="nextDay()">Next Day</button>
</body>
Run Code Online (Sandbox Code Playgroud)
JS
app.controller('MainCtrl', function($scope) {
$scope.myDate = new Date();
$scope.nextDay = function(){
$scope.myDate.setDate($scope.myDate.getDate() + 1);
};
});
Run Code Online (Sandbox Code Playgroud) 我有一个简单的输入列表绑定到一个显示良好的项目列表.当我更改输入中的值时,总和不会更新?
示例:http://plnkr.co/edit/B7tEAsXSFvyLRmJ5xcnJ?p = preview
HTML
<body ng-controller="MainCtrl">
<div ng-repeat="item in items">
<p><input type=text value="{{item}}"></p>
</div>
Total: <input type=text value="{{sum(items)}}">
</body>
Run Code Online (Sandbox Code Playgroud)
脚本
app.controller('MainCtrl', function($scope) {
$scope.items = [1,2,5,7];
$scope.sum = function(list) {
var total=0;
angular.forEach(list , function(item){
total+= item;
});
return total;
}
});
Run Code Online (Sandbox Code Playgroud) 我已经制作了一个AngularJS指令来为溢出添加省略号:隐藏文本.它似乎不适用于Firefox,我不相信我已经尽可能地构建它.流程是:
HTML
<p data-ng-bind="articleText" data-add-ellipsis></p>
Run Code Online (Sandbox Code Playgroud)
指示
app.directive('addEllipsis', function(){
return {
restrict : 'A',
scope : {
ngBind : '=' // Full-length original string
},
link : function(scope, element, attrs){
var newValue;
scope.$watch('ngBind', function () {
/*
* CODE REMOVED - Build shortened string and set to: newText
*/
element.html(newText); // - Does not work in Firefox and is probably not best practice
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
有问题的一行是指令中的最后一行:
element.html(newText)
Run Code Online (Sandbox Code Playgroud)
我假设应该使用一些模板式方法?我不清楚如何最好地接近答案.谢谢你的帮助.