我可以监视指令中属性的更改吗?我有这样的事情:<online-wathers></online-watchers>它向在线广播用户显示视频广播,并且需要一个广播ID。显然,没有此属性将无法正常工作。该视频广播不是在准备好文档时开始,而是在发布者实际想要开始时开始。当他开始时-后端给了我广播ID。现在,当api返回我broadcast-id时,我只需将其附加到具有broadcast-id =“ xx”的DOM上。$(".online-watchers-container") .html($("<online-watchers broadcast-id='"+broadcast.id+"' />"));$(".chat-container") .html($("<chat broadcast-id='"+broadcast.id+"' />"));$(".request-container") .html($("<live-requests broadcast-id='"+broadcast.id+"'></live-requests>"));// compile directivesangular.bootstrap(angular.element("online-watchers, chat, live-requests"), ['MyApp']);但是,有什么内部方法可以监视添加或更改属性吗?因此,在页面加载时,我已经在DOM中具有 ,并且当我获得与broadcastId的响应时,我只是将属性添加到DOM中的元素,并且指令对此做出了反应。请,如果您将其视为狗屎代码,如果您显示一些更好的解决方案的示例,我们将不胜感激。谢谢。 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 是的你可以function MyDirective() { return { link: function (scope, iElement, iAttrs) { scope.$watch(iAttrs.scopeProp, function (val) { console.log("prop: " + val); }); iAttrs.$observe("interpolated", function (val) { console.log("interpolate: " + val); }); } } }。<my-directive scope-prop="someId" interpolated="{{someId + 1}}"。function MyCtrl($scope) { $scope.someId = 12;}有关更多示例,请查看:How to get evaluated attributes inside a custom directive (adsbygoogle = window.adsbygoogle || []).push({});