- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我确实有一个指令,一旦文档准备好,我将尝试应用 Bootstrap 折叠功能。
为此,我必须浏览集合中的每个对象。
我的指令看起来像这样:
'use strict';
angular.module('app').directive("adScheduleTimeline",function($document) {
return {
restrict: 'E',
scope:{
ngModel: '='
},
templateUrl: '/partials/schedule/ad-schedule-timeline',
link: function (scope, el, attrs) {
scope.panelBaseId = attrs.collapsePanelBodyId;
scope.panelId = attrs.collapsePanelId;
$document.ready(function(){
console.log(scope.ngModel)
angular.forEach(scope.ngModel, function(value, key){
if (value.collapsed)
{
$("#" + scope.panelBaseId + "-" + key).collapse('show');
}
});
});
}
};
});
加载页面后,scope.ngModel
未定义,因此collapse
功能不起作用。有没有更好的方法在页面加载时实现类似的功能。
仅供引用,折叠函数来自 Bootstrap2.x.x。我没有使用 angular-ui-bootstrap。
最佳答案
您可以使用 scope.$watch()
等待 scope.ngModel
的值被填充。
link: function (scope, el, attrs) {
scope.panelBaseId = attrs.collapsePanelBodyId;
scope.panelId = attrs.collapsePanelId;
scope.$watch('ngModel', function (model) {
if (!model) { return; }
angular.forEach(model, function(value, key) {
if (value.collapsed) {
$("#" + scope.panelBaseId + "-" + key).collapse('show');
}
});
});
}
如果您只想执行一次,即稍后更改 scope.ngModel
时无需再次调用 collapse()
。您可以像这样取消观看:
link: function (scope, el, attrs) {
scope.panelBaseId = attrs.collapsePanelBodyId;
scope.panelId = attrs.collapsePanelId;
var unwatch = scope.$watch('ngModel', function (model) {
if (!model) { return; }
unwatch();
angular.forEach(model, function(value, key) {
if (value.collapsed) {
$("#" + scope.panelBaseId + "-" + key).collapse('show');
}
});
});
}
顺便说一句,$document.ready()
在这里没用, Angular 无论如何都会在开始 Bootstrap 之前等待文档准备好。
希望这有帮助。
关于javascript - $document.ready 和 ng-model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25182759/
根据 jQueryDoc在术语中,.ready() 被称为位于 jQuery.prototype 中的查询选择方法。对于前 $(document).ready() 作为jQueryDoc说:$ 命名空
我见过一些代码,他们只是这样做: $().ready(function() { ... }); 这比执行文档选择器要短,但它是同一件事吗? 最佳答案 略有变化: $(document).rea
各个处理程序的回调函数何时执行? 最佳答案 在主要过程中 当您的应用完成初始化并准备打开浏览器窗口时,将触发app上的“就绪”事件。由于在此之前您无法打开窗口,因此可以使用回调函数来创建Browser
最近发现了head.js图书馆和男孩,我对此感到满意,尽管我仍然对一件事感到有点困惑。 来自 headjs.com: The “DOM ready” event such as $(document)
我在我的代码库中发现代码在另一个 $(document).ready(function() { ... 例如 $(document).ready(function() { // 20
我已经养成了从ready函数开始jquery编码的习惯 $(function(){...}); 并将所有从ready调用的函数放入ready中。 然后我意识到,放入就绪函数中的一些函数可能不需要在那里
在浏览旧代码库时,我发现了以前的软件开发人员正在使用的地方 $(function(a) {}(A || (A = {}))); 即使页面尚未准备好,它仍在执行。在我删除传递的全局变量后它开始工作。代码
我已经看到了两种方式,但哪种更好或者并不重要。 我觉得包装每个语句可能会更干净,但只是想知道如果您有 50 个语句,每个语句都有自己的 document.ready 事件处理程序,是否会有更多回调?
这个问题已经有答案了: Four variations of jQuery ready() -- what's the difference? (4 个回答) 已关闭 4 年前。 我正在编写一些我没有
我们最近遇到了一个错误,jquery document.ready 似乎在 DOM 加载之前触发。事实证明,在调用的第一个项目之后有一些错误的代码 $.ready(function(){}); 这条语
什么应该在 jQuery.ready() 中,什么应该在 jQuery.ready() 之外? 从性能角度来看,我在某处读到,将所有代码包装在 jQuery.ready() 中并不是一种有效的方法。
我实现了一个带有选项列表(工作类别)的页面,单击该页面时应显示数据(工作描述)。我正在使用 BBQ 来处理后台堆栈。 一切正常,除了在用户首次导航到页面时设置初始选择。我的代码被调用到 addClas
解决方案 我有两个 users.js和 users.coffee在我的 Assets 管道中。显然 users.coffee正在阻止 users.js从被加载。确保删除它! 我正在尝试实现 this
我正在研究一个指令,但我不想玩 $document或 $window , 只有 element本身。 之前我有: angular.element($document).ready(function()
我想在移动设备方向改变时使用 $(window).resize 来调用一些函数,我在 $(document).ready 中编写了所有代码,这在我使用 Android 设备时有效,但在使用 iPhon
我有一个使用数据库的 Ionic 应用程序。我想用一个文件的内容填充这个数据库。 这部分我开始工作了。我想创建一个 DB.ready() 事件,很像 $ionicPlatform.ready() 或
我有一个名为“loadTimeTrackersGrid()”的函数,它加载一个弹性网格。设置如下所示: $(document).ready(function () { var edi
我有一个定义数量为 replicas 的部署.如果我的 Pod 准备好/未准备好处理新连接,我使用就绪探针进行通信 - 我的 Pod 在 ready 之间切换/not ready他们一生中的状态。 我
有什么区别: $(document).ready(initialize); 和 $(document).on('ready', initialize); 对我来说,它们似乎以相同的方式工作。 最佳答案
我看到很多项目都在使用 $(document).on('ready', function(e){ //jquery stuff }) 而不是: $( document ).ready(functio
我是一名优秀的程序员,十分优秀!