要麼你做的不正確,要麼是角度刷新問題。
對於第一種情況,如果您提供一個codepen以便我們可以看到問題,則會更容易。目前您提到的代碼對我來說工作正常here。
對於第二種情況,實際上第三行ons.compile(content);
應該刪除這個問題imo,它可能是一個版本問題,或者有一些我缺少的上下文。
如果你正在做角度相關的事情,那麼你也應該顯示你從哪裏調用這3條線。爲了工作,應該從類似於ng-
事件(例如ng-click
)的事件中調用它。
JS:
app.controller('yourControllerName', function($scope) {
$scope.addButton = function() {
var content = document.getElementById("my-content");
content.innerHTML="<ons-button>Another Button</ons-button>";
ons.compile(content);
}
});
HTML:
<ons-button ng-click="addButton()">Add another button</ons-button>
最後,如果你不能讓它工作,你可以這樣做
app.controller('yourControllerName', function($scope) {
$scope.addButton = function() {
var content = document.getElementById("my-content");
content.innerHTML="<ons-button>Another Button</ons-button>";
ons.compile(content);
$scope.apply(); // should fix the issue, but not recommended
}
});
PS:你也可以嘗試溫泉2 - 你可以:
問題