0

I would like to create a list of items built in Directive and share through controllers.

Here is my example in plunker: Example of my code

Here is my code in js:

var app = angular.module('app', []);

app.controller("BrunchesCtrl", function ($scope, $log) {
    $scope.brunches = [];

    $scope.addNew = function () {
        $scope.brunches.push({ address: "" });
    };
    $scope.$watch('brunch.address', function(value) {
        $scope.address = value;
        $log.info($scope.address);
    });

    $scope.$watch(function() { return $scope.brunches.length; }, function() {
        $scope.brunches = $scope.brunches.map(function (brunch) {
            return brunch;
        });
    });
});

app.directive('brunchesView', function ($compile) {
    return {
        restrict: 'E',
        templateUrl: 'brunch.html',
        controller: 'BrunchesCtrl',
        replace: true,
        link: function (scope, element, attrs, controller) {

        }
    };
});

app.directive('businessSubmit', function ($log, $compile, formManagerService) {
    return {
        restrict: 'AE',
        controller: 'BrunchesCtrl',
        link: function (scope, element, attrs, controller) {
            formManagerService.init();
            var hasError;

            element.on('click', function (e) {
                e.preventDefault();
                $log.info("brunches: \n");
                $log.info(scope.brunches);
            });
        }
    };
});

Here is an HTML:

<!DOCTYPE html>

          <div class="controls">
               <a class="btn btn-danger" id="addBrunch" data-ng-click="addNew()">
                  <i class="icon-plus-sign"></i>
                  add new...
              </a>
          </div>
      </div>

      <brunches-view class="well" data-ng-repeat="brunch in brunches">{{brunch}}</brunches-view>

      <br/>
      <p class="well">
          JSON: {{brunches | json}}
      </p>

      <div class="control-group">
          <div class="controls">
              <a class="btn btn-primary" href="#" data-business-submit>
                  <i class="icon-save"></i>
                  Save...
              </a>
          </div>
      </div>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>      
  <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
  <script src="script.js"></script>

And here is the template:

<div class="fc-border-separate">
    <div class="control-group">
        <label class="control-label">Address</label>
        <div class="controls">
            <input type="text" class="span6 m-wrap"
                   name="Address" data-ng-model="address" 
                   value="{{address}}" />
        </div>
    </div>
</div>

The final thing I want to save the whole data inside the BusinessSubmit directive.

Help please...

4
  • it there a special reason why you want the submit happen in a directive? How about a simple controller function, that get's called on click? Commented Jan 5, 2014 at 14:10
  • I agree with @mseemann. It sounds odd what you are trying to achieve. Commented Jan 5, 2014 at 14:24
  • Can you show an example? Commented Jan 5, 2014 at 14:26
  • @IamStalker: plnkr.co/edit/… Commented Jan 5, 2014 at 14:41

1 Answer 1

1

Several issues with your code.

First, your ng-model for the <input> is set to address, but the object you are wanting to bind it to a brunch object that has an address property. Important to realize that ng-repeat will create a child scope for every repeated item

<input data-ng-model="brunch.address"/>

Another issue is you are assigning the parent controller to a directive as well. Important to understand that controllers are singletons so if you use controller more than once , each is a separate instance. Therefore nesting the parent controller in a directive makes no sense.

DEMO- [ng-model] fix

If you want the data shared with other controllers you should set up a service that holds the brunches data by injecting it into whatever controllers will need access

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.