我有一个AngularJS指令的特定场景:
$scope.myValues
中的所有值替换为myValues
(从网络服务加载的对象)我无法在这种情况下更改主范围,因为它由另一个应用程序(或多或少是一个插件机制)拥有。
谢谢&问候 斯蒂芬
答案 0 :(得分:1)
如果认为我找到了解决方案:
示例Html:
<wi-view data-layout="{{passLayout}}"></wi-view>
<hr />
Original property: {{layout.property1}}
示例控制器:
app.controller('wiController', function($scope) {
// Simulating the original scope values
$scope.layout = {};
$scope.layout.property1 = 'Original Value';
// New scope values, just here for binding it to the controller
var passLayout = {};
passLayout.property1 = 'Value Overwritten';
passLayout.property2 = 'Another Property';
$scope.passLayout = passLayout;
});
样本指令:
app.directive('wiView', function () {
var linkFunction = function(scope, elems, attrs) {
if (attrs.layout !== undefined) {
scope.layout = angular.fromJson(attrs.layout);
}
};
return {
restrict: "E",
scope: true,
priority: 0,
link: linkFunction,
template: '<div>Hello, {{layout.property1}}!</div>'
};
});