Giter Club home page Giter Club logo

Comments (6)

VanTanev avatar VanTanev commented on May 24, 2024

Sorry, this does not pertain to angular-chosen. Try the angular mailing list or #angularjs irc channel.

from angular-chosen.

VanTanev avatar VanTanev commented on May 24, 2024

But, let me give you a pointer, just because:

It looks to me as if you're writing "jquery-in-angular" instead of writing angular code.

If you supply a complete specification of what you're building, I could give you a better pointer, but it sounds to me like you have an ajax that returns a data structure like:

{
  "inputs": [
    { "name": "my-input", "values": ["one", "two", "three"] }
  ]
}

( or at least, something to that effect).

A way to handle this in Angular would be:

// in controller or directive
$scope.models = {}
$http.get('/api/endpoint').then(res => res.data).then(data => {
  $scope.inputs = data.inputs
})
// in template
<div ng-repeat="input in inputs">
  <label>{{ ::input.name }}</label>
  <select chosen ng-model="models[input.name]" ng-options="value as value for value in input.values"></select>
</div>

from angular-chosen.

 avatar commented on May 24, 2024

Hi @VanTanev, thank you for the pointer. Regarding the code, the definition of the drop down itself is coming from the API, and cannot be written in the template in advance. It seems to me directly related to angular chosen (have you tried it?) I will check with the angular group. Thanks!

from angular-chosen.

VanTanev avatar VanTanev commented on May 24, 2024

Hey @dwilliams999, it's fine if the definition comes from the API - the point of angular templates is to be programmable in and of themselves. You definitely do not need to manually compile and insert dom fragments to get this to work.

I have had to implement something similar in the past, it looked like this:

  # Options are a hash like::
  # {
  #   "option_name" => {
  #     "value" => "option value",
  #     "type" => "boolean/time/integer..etc",
  #     "label" => "Fancy label",
  #     (opt) "choices" => { "choice_val" => "Choice Label" }, # for type "selection"
  #     "help", "group", "position", "hidden", "allow_per_symbol", # field options, false if not set
  #   },
  #   (opt) "errors" => { "option_name" => ["array", "of", "error", "messages"] },
  # }
<div class="form-group" show-errors
     ng-repeat="(option_name, option) in options"
     ng-class="{'has-error': options.errors[option_name]}"
     ng-if="!option.hidden"
     >
    <label class="col-md-3 control-label" for="{{ ::option_name }}">{{ ::option.label }}:</label>
    <div class="col-md-8">
        <div ng-switch on="option.type">
            <select ng-switch-when="boolean"
                ng-options="opt.value as opt.label for opt in [{label: 'YES', value: true}, {label: 'NO', value: false}]"
                ng-model="option.value"
                id="{{ ::option_name}}"
                class="form-control"
                name="{{ ::option_name}}"
                required=required
            ></select>

            <input ng-switch-when="percent"
                ad-float
                type="text"
                ng-model="option.value"
                id="{{ ::option_name}}"
                class="form-control"
                name="{{ ::option_name }}"
                required=required
            />

            <input ng-switch-when="integer"
                ad-integer
                type="number"
                ng-model="option.value"
                id="{{ ::option_name}}"
                class="form-control"
                name="{{ ::option_name}}"
                required=required
            />

            <ad-time ng-switch-when="time"
                    ng-model="option.value"
                    ></ad-time>

            <select ng-switch-when="selection"
                chosen
                class="form-control"
                id="{{ ::option_name}}"
                name="{{ ::option_name}}"
                ng-model="option.value"
                ng-options="key as value for (key, value) in option.choices"
                required=required
                ></select>

            <input ng-switch-default
                type="text"
                ng-model="option.value"
                id="{{ ::option_name}}"
                class="form-control"
                name="{{ ::option_name}}"
                required=required
            />
        </div> <!-- ng-switch -->

        <span ng-if="::option.help"
                class="help-block"
                ng-bind="option.help"
                ></span>

        <span class="help-block"
                ng-show="options.errors[option_name]"
                ng-repeat="error in options.errors[option_name]"
                ng-bind="error"
                ></span>
    </div>
</div>

from angular-chosen.

 avatar commented on May 24, 2024

@VanTanev, thank you for the code sample. I understand the approach you are illustrating. Let me see if that works and I will get back with you here.

from angular-chosen.

 avatar commented on May 24, 2024

Works like a charm, thanks @VanTanev !

from angular-chosen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.