0

I have $scope.loaded=false; in controller. Seems like directive doesn't pick up the scope, because ng-show="loaded" still shows DIV. But when I click button and controller changes $scope.sent to true, directive gets scope(it changes class as seen in directive). Why is directive not picking $scope.loaded?

Message directive is loaded in other view:

<form class="form-horizontal" ng-submit="submit()">
    <fieldset>

        <!-- Form Name -->
        <legend>Contact us</legend>

        <!-- Text input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="textinput">Your name</label>
            <div class="col-md-4">
                <input id="name" name="textinput" type="text" placeholder="name" class="form-control input-md" ng-model="model.name" required>
            </div>
        </div>

        <!-- Text input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="textinput">Your email</label>
            <div class="col-md-4">
                <input id="from" name="textinput" type="email" placeholder="email" class="form-control input-md" ng-model="model.from" required>
            </div>
        </div>

        <!-- Textarea -->
        <div class="form-group">
            <label class="col-md-4 control-label" for="textarea">Content</label>
            <div class="col-md-4">
                <textarea class="form-control" id="content" name="textarea" ng-model="model.content" required>your message to us</textarea>
            </div>
        </div>

        <!-- Button -->
        <div class="form-group">
            <label class="col-md-4 control-label" for="singlebutton"></label>
            <div class="col-md-4">
                <button id="singlebutton" name="singlebutton" class="btn btn-primary" ng-click>Submit</button>
            </div>
        </div>

        <message-directive></message-directive>

    </fieldset>
</form>

I have a custom directive:

(function (angular) {
    angular.module('app')
           .directive('messageDirective', [function () {
               return {
                   restrict: 'E',
                   templateUrl: 'partials/message-directive',
                   scope: true,
                   link: function (scope) {
                       scope.loaded = true;
                   }
               };
           }]);
})(angular);

TemplateUrl:

<div class="form-group">
    <label class="col-md-4 control-label" for="message"></label>
    <div class="col-md-4">
        <div ng-show="loaded" ng-class="sent ? 'alert alert-success' : 'alert alert-danger'">
            {{message}}
        </div>
    </div>
</div>

Controller:

(function (angular) {
    angular.module('app')
        .controller('contactController', [
            '$scope', 'contactService', function ($scope, contactService) {

                $scope.model = {};
                $scope.loaded = false;

                var successCallback = function () {
                    $scope.message = "Sent!";
                    $scope.sent = true;
                }

                var errorCallback = function () {
                    $scope.message = "Error!";
                    $scope.sent = false;
                }

                $scope.submit = function() {
                    contactService.createContact($scope.model).then(successCallback, errorCallback);
                }

            }]);
})(angular);
3
  • when you click what button? I don't see a button. Commented Sep 27, 2016 at 19:32
  • 1
    Where is the message directive? It seems your html doesn't include it anywhere. Commented Sep 27, 2016 at 19:34
  • I've updated my question with controller and view where i call directive Commented Sep 27, 2016 at 21:28

2 Answers 2

0

the directive load after the controller .so your code
link: function (scope) { scope.loaded = true; } rewrite the $scope.loaded

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

5 Comments

what would be the way to make directive act after button click?
why you need to set scope.loaded as true in directive?
because i want to reuse this directive in multiple different pages where i submit button, for same message display. so before button is clicked message is hidden, once button is clicked and promise is done i should show message. i want to make it reusable, not to change $scope.loaded each time in controller.
each directive has its own scope,so you just need to set the scope.loaded as false in your directive once,they won't effect each other,you even don't need to init $scope.loaded in your controller. see this plunker
ok, but can you give example how i could make $scope.loaded change to true after promise in controller has returned without doing it in controller, and using link: or controller: of custom directive? Thanks for sticking with me.
0
return {
               restrict: 'E',
               templateUrl: 'partials/message-directive',
               scope: false,
               link: function (scope) {
                   scope.loaded = true;
               }
           };

1 Comment

When scope is set to “false”, the controller and directive are using the same scope object. This means any changes to the controller or directive will be in sync. Now if scope.loaded value changes either in controller or directive it will be in sync

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.