0

I have a view called 129.html and it's format is like following. It's basically bunch of questions which are reading from json file.

<div class="" ng-repeat="group in groups">
        <h2 ng-bind="group.title"></h2>
        <div class="" ng-repeat="section in group.sections">
            <div class="" ng-repeat="field in section.fields">

                <!-- textfield -->
                <div class="form-group" ng-class="{ 'has-error': form.$submitted && form[field.id].$invalid }" ng-if="field.type === 'text'">
                    <!-- <label for="{{field.id}}">{{field.title.substr(field.title.indexOf('.') + 2)}}</label><br> -->
                    <label for="{{field.id}}">{{field.title}}</label>
                    <br>
                    <input type="text" class="form-control" id="{{field.id}}" name="{{field.id}}" ng-model="formData[field.id]" ng-required="field.validations.required" ng-minlength="field.validations.min_length">
                    <p class="form-group-note" ng-if="field.info" ng-bind="field.info"></p>

                    <div ng-show="form.$submitted" ng-cloack>
                        <span class="help-block" ng-show="form['{{field.id}}'].$error.required" ng-if="field.validations.required">Please enter a value, this field is required</span>
                        <span class="help-block" ng-show="form['{{field.id}}'].$error.minlength" ng-if="field.validations.min_length">Please enter a value of at least {{field.validations.min_length}} characters</span>
                    </div>
                </div>
     </div>
</div>

Ana I have MainController.js which has functionality of saving users' input in localstorage in realtime. However, it seems like my 129.html is not recognizing that maincontroller.How can I add this to the view? I've seen something like ng-controller=MainController but i'm not sure whether this is correct.

1
  • Please provide more details, there is not enough information to help you. Commented Apr 15, 2016 at 8:03

1 Answer 1

1

If you're using a router you can link your controller to the view otherwise the ng-controller directive is a good way to do it.

You cannot associate the "ng-controller" directive with the "ng-repeat" directive because it will call the controller for each group so you need to encapsulate all of it in a parent tag such as a div :

<div ng-controller="MainController">
<div class="" ng-repeat="group in groups">
  (...)
</div>
</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.