1

I have a specific scenario for a AngularJS directive:

  • Normally the directive should inherit the default scope
  • But for some specific scenarios I'd like to replace all values in $scope.myValues with myValues (object loaded from a web-service)

I cannot change in this scenario the main-scope because this is owned by another application (more or less a plugin-mechanism).

Thanks & Regards Stefan

4
  • Can't you simply set your $scope.myValues = MyDirective.getNewValues() within your $http web service success? Commented Jun 10, 2014 at 17:31
  • 3
    Can you post some example code showing what have you tried? Commented Jun 10, 2014 at 17:36
  • Please answer your own question rather than editing the answer into the question. Commented Jun 10, 2014 at 22:11
  • @Wex: OK, yes, you are right, done. Commented Jun 11, 2014 at 7:17

1 Answer 1

1

If think I have found the solution:

Sample Html:

<wi-view data-layout="{{passLayout}}"></wi-view>
<hr />
Original property: {{layout.property1}}

Sample Controller:

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;

});

Sample Directive:

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>'
};

});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.