0

I am trying to make a simple working hours directive using angularjs. My modal array which looks like this

scope.model = [
    { name: 'Sunday', selected: false, openhoursFrom: '', openhoursTo: '' },
    { name: 'Monday', selected: false, openhoursFrom: '', openhoursTo: ''  },
    { name: 'Tuesday', selected: false, openhoursFrom: '', openhoursTo: ''  },
    { name: 'Wednesday', selected: false, openhoursFrom: '', openhoursTo: ''  },
    { name: 'Thursday', selected: false, openhoursFrom: '', openhoursTo: ''  },
    { name: 'Friday', selected: false, openhoursFrom: '', openhoursTo: ''  },
    { name: 'Saturday', selected: false, openhoursFrom: '', openhoursTo: ''  },
];

What I wanted to do is when a user clicks on a specific checkbox, it suppose to update the that particular day selected: true. I cant seem to achieve this instead when i click is either nothing happens or everything is selected. What should I do to fix this problem?

Here is the link to the plunkr demo.

2 Answers 2

3

Angular uses camelcase for directive definitions but dashes in html markup. See the doc on normalization

We typically refer to directives by their case-sensitive camelCase normalized name (e.g. ngModel). However, since HTML is case-insensitive, we refer to directives in the DOM by lower-case forms, typically using dash-delimited attributes on DOM elements (e.g. ng-model).

You should be using

ng-model="day.selected"

and not

ngModel="day.selected"

See working forked plunker.

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

Comments

0

Please add the checkbox change event to update the model selected state

elem.bind('change', function (evt) {
        angular.forEach(scope.model, function (v) {
            if (v.name == evt.target.value) {
              v.selected = evt.target.checked;
              scope.$apply();
            }
        });
      });

you can see the full code here

1 Comment

This is the worst approach one could imagine for Angular app. There are directive that help with binding, which is the strong part of Angular. But making model updates manually eliminates the reasons to use Angular as framework...

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.