我正在尝试编辑ng-controller
元素外部的视图。我能够通过使用$rootScope
和 dom 操作来解决它,但我想知道它是如何通过原生 angularjs 来解决的?
html:
<body>
<div class="container">
<div class="block" ng-controller="BlockController as block">
<div><strong>Name:</strong> {{ block.name }}</div>
<a href ng-click="block.edit()">Edit</a>
</div>
</div>
<div class="container-editor">
<div id="block-editor"></div>
</div>
</body>
JS:
angular.module('app', [])
.controller('BlockController', BlockController);
function BlockController($compile, $scope)
{
this.name = 'default name';
this.edit = function() {
var $editor_html = ' <input type="text" ng-model="block.name" name="name" /> <a href ng-click="block.save()">Save</a>';
$editor_html = $compile($editor_html)($scope);
angular.element(document.querySelector("#block-editor")).html('').append($editor_html);
};
this.save = function() {
// save block
angular.element(document.querySelector("#block-editor")).html('');
};
}
这是示例