if statement - if else condition in angularjs to compare string and int value -
i want check if both font , fontsize undefined sent client side.when user not select font or font size, pass value undefined. these conditions work when 1 of them (font or font size) undefined, when both of them undefined, recieve error,
typeerror: cannot read property 'font' of undefined @ scope.stylesctrl.$scope.addstyles (http://0.0.0.0:8080/app/edit/styles/stylesctrl.js:26:38) @ fn (eval @ <anonymous> (http://0.0.0.0:8080/bower_components/angular/angular.js:13231:15), <anonymous>:4:317) @ ngeventdirectives.(anonymous function).compile.element.on.callback (http://0.0.0.0:8080/bower_components/angular/angular.js:23411:17) @ scope.$get.scope.$eval (http://0.0.0.0:8080/bower_components/angular/angular.js:15916:28) @ scope.$get.scope.$apply (http://0.0.0.0:8080/bower_components/angular/angular.js:16016:25) @ htmlinputelement.<anonymous> (http://0.0.0.0:8080/bower_components/angular/angular.js:23416:23) @ htmlinputelement.jquery.event.dispatch (http://0.0.0.0:8080/bower_components/jquery/dist/jquery.js:4435:9) @ htmlinputelement.jquery.event.add.elemdata.handle (http://0.0.0.0:8080/bower_components/jquery/dist/jquery.js:4121:28) undefined
below if else conditions in server side,
if(font == undefined && fontsize == undefined){ //doesn't work console.log("font"); updatefile(main, [ {rule: ".made-easy-themecolor", target: "color", replacer: color} ], function (err) { console.log((err)); }); } else if(font == undefined){ //work updatefile(main, [ {rule: ".made-easy-themecolor", target: "color", replacer: color}, {rule: ".made-easy-themefontsize", target: "font-size", replacer: fontsize + "em"} ], function (err) { console.log((err)); }); } else if(fontsize == undefined){ //work updatefile(main, [ {rule: ".made-easy-themecolor", target: "color", replacer: color}, {rule: ".made-easy-themefont", target: "font-family", replacer: font} ], function (err) { console.log((err)); }); } else{ //work updatefile(main, [ {rule: ".made-easy-themecolor", target: "color", replacer: color}, {rule: ".made-easy-themefont", target: "font-family", replacer: font}, {rule: ".made-easy-themefontsize", target: "font-size", replacer: fontsize + "em"} ], function (err) { console.log((err)); }); }
this html code in front end
<h3 ng-style="{'font-family': styles.textsize.font, 'font-size': styles.textsize.size + 'px'}">text is</h3>
this controller send data server
$scope.addstyles = function(styles) { $scope.fontdata = { appid: "55c0ace94aa248735d75b140", header: styles.uploadme, color: styles.mycolor, font: styles.textsize.font, fontsize: styles.textsize.size }; stylesservice.editstyles($scope.fontdata) .success(function (res) { console.log("success"); }) };
is there problem if else statements? or because comparing string value(font) , int value(fontsize)?
the problem you're not initializing model objects on $scope
. has nothing w/your server side if/else... @hrishi points out, it's javascript error :)
to fix it, need initialize object styles.textsize
on scope.
the reason angular (ng-model
?) create object when user types 1 of inputs. when user doesn't type anything, styles.textsize
never gets defined.
just guessing here, b/c can't see template or rest of code in controller, fix this:
$scope.styles = { textsize: { } };
Comments
Post a Comment