formly-js / angular-formly

JavaScript powered forms for AngularJS
http://docs.angular-formly.com
MIT License
2.22k stars 404 forks source link

Using a function in expressionProperties.templateOptions.disabled #238

Closed icfantv closed 9 years ago

icfantv commented 9 years ago

I'm using the bootstrap templates (so I apologize in advance if this is a template issue) and it appears that if I specify a function as follows:

...
expressionProperties: {
  'templateOptions.disabled': 'foo()'
}

The function is not actually executed. Like, ever.

Is this by design? Is this a limitation in formly? Am I doing something wrong? Thanks.

kentcdodds commented 9 years ago

The expressionProperties are formlyExpressions. See the docs here: http://docs.angular-formly.com/v5.2.1/docs/formly-expressions

kentcdodds commented 9 years ago

Let me know if you need more assistance :-)

icfantv commented 9 years ago

Thanks! Will give this a go.

icfantv commented 9 years ago

Worked perfectly (I still don't know why I sometimes am surprised when that happens). Thanks again.

hmendezm commented 9 years ago

Hi icfantv,

I am new in formly and I tring to implement multilingual UI so I need to run a function in the 'templateOptions.labelProp' so I have the following

 {
            key: 'country',
            type: 'select',
            templateOptions: {
                label: 'Country of operation',

                options: [],
                valueProp:'id',
                labelProp:'code',
                required: false
            },
                expressionProperties: {
                        'templateOptions.labelProp': function($viewValue, $modelValue, scope) {
                            return "_TESTFUNCTION";
                        }
                },
                controller: function($scope,$log, CountryService) {

                    CountryService.get().then(function (results) 
                        {
                            if(results)
                            {
                             $scope.options.templateOptions.options = results.data;
                             return results.data;
                            }
                        });  
                }
        }

I trying to use a function in the expressionProperties and it is not working. Can you help me in giving me a example how you did, please?

kentcdodds commented 9 years ago

@hmendezm, the best way to get help is to create an example using http://help.angular-formly.com. Just looking at your code, it appears that it should work fine. So an example is more helpful.

hmendezm commented 9 years ago

This is the example. If you see the values are undefine http://jsbin.com/vocexa/edit?js,output

I do not know what I am doing wrong

best regards

kentcdodds commented 9 years ago

Wait, so are you trying to do this?

hmendezm commented 9 years ago

Hi Kent, No, I do not want to change the title of the dropdown. I am try to change the label of the items in the dropdown list when user change the language. So my guess was to use a function that allow me to replace the value of the language. let me give you a example:

English Spanish Dominic Republic República Dominicana

In the dropdown will have english text (eg. Dominic Republic) initially but if user decides to change to Spanish text the label should be changed (eg. Republica dominicana)

In a select what usually I have in the html is the following

 <select class="form-control" ng-model="assesstment.Country" ng-options="country.id as country.translations[getLanguageID(country.translations,selectedLanguageCode,'language_id')].name for country in countries" ng-change="changedcountry(assesstment.Country)" >  
                            </select>  
The function getLanguageID will just to return value between 0 to 5 which is the index in the array (country.translations) that is location of the selected language.

JSON

[{"id":1,"region_id":1,"code":"Afghanistan","translations":[{"country_id":1,"language_id":1,"name":"Afghanistan"},{"country_id":1,"language_id":2,"name":"Afghanistan"},{"country_id":1,"language_id":3,"name":"Afganistán"},{"country_id":1,"language_id":4,"name":"Afghanistan"},{"country_id":1,"language_id":5,"name":"Afeganistão"},{"country_id":1,"language_id":6,"name":"Afghanistan"}]},
{"id":136,"region_id":5,"code":"Albania","translations":[{"country_id":136,"language_id":1,"name":"Albania"},{"country_id":136,"language_id":2,"name":"Albanie"},{"country_id":136,"language_id":3,"name":"Albania"},{"country_id":136,"language_id":4,"name":"Albanien"},{"country_id":136,"language_id":5,"name":"Albânia"},{"country_id":136,"language_id":6,"name":"Albania"}]},
{"id":48,"region_id":2,"code":"Algeria","translations":[{"country_id":48,"language_id":1,"name":"Algeria"},{"country_id":48,"language_id":2,"name":"Algérie"},{"country_id":48,"language_id":3,"name":"Argelia"},{"country_id":48,"language_id":4,"name":"Alrgerien"},{"country_id":48,"language_id":5,"name":"Argélia"},{"country_id":48,"language_id":6,"name":"Algeria"}]},
{"id":137,"region_id":5,"code":"Andorra","translations":[{"country_id":137,"language_id":1,"name":"Andorra"},{"country_id":137,"language_id":2,"name":"Andorre"},{"country_id":137,"language_id":3,"name":"Andorra"},{"country_id":137,"language_id":4,"name":"Andorra"},{"country_id":137,"language_id":5,"name":"Andorra"},{"country_id":137,"language_id":6,"name":"Andorra"}]}]

I hope this explains my problem/situation.

Thank you very much for reply and take time in this Best regards Henry

kentcdodds commented 9 years ago

Ah, maybe this will point you in the right direction: http://jsbin.com/yubato/edit?js,output

hmendezm commented 9 years ago

Thanks Kent, I will check the link and see how I can put translate to work :-) if I am successful I will create an example using formly, so the other developers who need translate can use.

Thanks again Have a nice day! H

rahulbhaya commented 8 years ago

How do you specify which option to disable?