如何创建更新AngularJS输入元素并更新范围的书签?

时间:2017-02-15 11:34:42

标签: javascript html angularjs bookmarklet

当我运行此bookmarklet JavaScript时,模型不会更新角度范围。如何触发范围更新或摘要周期,以便视图模型知道更新的输入元素值?

//This doesn't work, but it does update the input box:

javascript:(function(){
    var elementText = 'Changed text';
    var inputElement = document.getElementById('inputBox');
    inputElement.value = elementText;
})();

1 个答案:

答案 0 :(得分:0)

我发现触发元素更改事件会触发Angular摘要周期:

javascript:(function(){
    var elementText = 'Changed text';
    var inputElement = document.getElementById('inputBox');
    inputElement.value = elementText;
    if ("createEvent" in document) { 
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent("change", false, true);
        inputElement.dispatchEvent(evt); 
    } else {
        inputElement.fireEvent("onchange");
    }
})();