Resolve Circular Dependency Ionic

Some time in angular we face a error with name Circular Dependency, actually angular/ionic throw this error when we inject factory-1/service-1 in factory-2/service-2 and in factory-2/service-2 inject factory-1/service-1 like :


app.factory('factory1', function (factory2) {
    // Here is your other code
});

app.factory('factory2', function (factory1) {
// Here is your other code

});

We can resolve this error using $injector , like :

app.factory('factory1', function (factory2) {
    // Here is your other code
});

app.factory('factory2', function ($injector) {
var factory1 = $injector.get('factory1'); // Here is your factory1
// Here is your other code

});

input not showing invalid model values

The bug can be easily illustrated using the following example code :

<div ng-init="letters='1'">
letters = {{'' + letters}}
<input type="text" ng-model="letters" ng-pattern="/^[a-zA-Z]*$/" />
</div>

for resolve the error just user ng-model-options="{ allowInvalid: true }"  like :

<input type="text" ng-model="letters" ng-pattern="/^[a-zA-Z]*$/" ng-model-options="{allowInvalid: true}" />

Ionic Serve error

Full Error :

ionic $ module.js:338
    throw err;
          ^
Error: Cannot find module 'bower'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object. (/Users/jaswantdhayal/Desktop/Projects/Purpose it all/purposit/gulpfile.js:3:13)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)



Solution : => Go to the project directory

$rm -rf node_modules
$npm install

Convert Image into base64 by JS

When we want to upload a image in on server in cordova/ionic some time we need to convert the image in base64 formate . I've write a function which will convert your image in base64.

function convertImgToBase64URL(url, callback, outputFormat) {
 var img = new Image();
 img.crossOrigin = 'Anonymous';
 img.onload = function() {
  var canvas = document.createElement('CANVAS'),
      ctx = canvas.getContext('2d'),
      dataURL;
  canvas.height = this.height;
  canvas.width = this.width;
  ctx.drawImage(this, 0, 0);
  dataURL = canvas.toDataURL(outputFormat);
  callback(dataURL);
  canvas = null;
 };
 img.src = url;
 return url;
}

How to use :

convertImgToBase64URL(img, function(base64Img) {
 console.log(base64Img);// this is your base64 converted image     
});

Post this image on server(IONIC)

$http.post(server_url,[base64Img])

Handel this image in PHP :

   
$params = json_decode(file_get_contents('php://input'), true); get the post image

$img = imageUploadByBase64($params[0]);// this is the saved image path with image name

function imageUploadByBase64($img) {
 $randomImgName = date('Y-m-d_H-i-s').'_'.rand(0, 6000).'.png';
 $image = base64_to_jpeg($img, 'uploads/' . $randomImgName);// you need to create a directory withh name 'uploads'
 return $image;
}

function base64_to_jpeg($base64_string, $output_file) {
 $ifp = fopen($output_file, "wb");
 $data = explode(',', $base64_string);
 fwrite($ifp, base64_decode($data[1]));
 fclose($ifp);
 return $output_file;
}

Android Runtime error E/AndroidRuntime: FATAL EXCEPTION: main



   java.lang.RuntimeException: Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.IllegalStateException: Incorrect provider authority in manifest. Most likely due to a missing applicationId variable in application's build.gradle.
   at android.app.ActivityThread.installProvider(ActivityThread.java:5236)
   at android.app.ActivityThread.installContentProviders(ActivityThread.java:4828)
   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4768)
   at android.app.ActivityThread.access$1600(ActivityThread.java:154)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:152)
   at android.app.ActivityThread.main(ActivityThread.java:5497)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Incorrect provider authority in manifest. Most likely due to a missing applicationId variable in application's build.gradle.
   at com.google.firebase.provider.FirebaseInitProvider.zza(Unknown Source)
   at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
   at android.app.ActivityThread.installProvider(ActivityThread.java:5233)
   at android.app.ActivityThread.installContentProviders(ActivityThread.java:4828) 
   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4768) 
   at android.app.ActivityThread.access$1600(ActivityThread.java:154) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:152) 
   at android.app.ActivityThread.main(ActivityThread.java:5497) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 




Add a single line in your build.gradle(yourProject/platforms/android/build.gradle) file 

   below   defaultConfig {

com.your.app <= Replace this with Your app bundle identifier which you can fine in you config.xml file


    defaultConfig {     
     applicationId "com.your.app" //<== Just add this line in you build.gradle file