0

I am tying to set the input field dynamically. But I came up with error. how to use the ng-switch-when to set the type in input fields or any other alternate?

here is my temp:

var GridTemplate = [
            '<div>',
                '<div>',
                    '<ul class="titles"><li  ng-repeat="page in currentPage">{{page.title}}</li></ul>',
                    '<div class="rowContent">',
                    '<ul ng-repeat="(title,page) in currentPage">',
                        '<li  ng-repeat="(key,element) in page.key track by $index">',
                        '<input ng-switch-when="page.key[key] == string" type="text" name="" id="" ng-model="page.key[key]" ng-blur="numSort( page )" />',
                        '<input ng-switch-when="page.key[key] == number" name="" id="" ng-model="page.key[key]" ng-blur="numSort( page )" />',
                        '</li></ul></div>',
                    '<div class="pageNavigator"><ul><li  ng-repeat="page in slides"><a ng-href="">{{$index+1}}</a></li></ul></div>',
                '</div>',
            '</div>'
            ];
2
  • What error are you getting Commented May 19, 2017 at 14:08
  • error is : Controller 'ngSwitch', required by directive 'ngSwitchWhen', can't be found! Commented May 19, 2017 at 14:08

2 Answers 2

1

Try this:

<input type="{{getInputType(page.key[key])}}" ng-model="page.key[key]" />

and then a function in your controller to check the param and return the input type

 $scope.getInputType = function(param){
     if(angular.isString(param)) return "text";
     if(angular.isNumber(param)) return "number";
 }
Sign up to request clarification or add additional context in comments.

Comments

0

ng-switch-when directives need to be wrapped in a ng-switch directive.

From here:

<ANY ng-switch="expression">
  <ANY ng-switch-when="matchValue1">...</ANY>
  <ANY ng-switch-when="matchValue2">...</ANY>
  <ANY ng-switch-default>...</ANY>
</ANY>

So in your case I think it would be smth like

<li ng-switch="page.key[key]" ng-repeat="(key,element) in page.key track by $index">
  <input ng-switch-when="string" type="text" name="" id="" ng-model="page.key[key]" ng-blur="numSort( page )" />
  <input ng-switch-when="number" name="" id="" ng-model="page.key[key]" ng-blur="numSort( page )" />
</li>

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.