2

I am not sure where the error is but my angularjs is not being detected.

Below is my HTML and Angularjs

<apex:page controller="ApexRemoteInvokeManagerController"    
 docType="html-5.0" sidebar="false">
 <html>
 <head>
  <link href="//cdn.jsdelivr.net/webjars/bootstrap-sf1/0.1.0-   
beta.6/css/bootstrap-namespaced.css" rel="stylesheet"/>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-   
beta.11/angular.min.js"></script>
  <script>
   var accountApp = angular.module('accountApp',[]);
      accountApp.factory('accountAndContactsFactory', ['$q',   
'$rootScope', function($q, $rootScope) {
        this.save =  function() {
            var deferred = $q.defer();
            var contactJson = JSON.stringify(contact);
            var accountJson = JSON.stringify(account);
            Visualforce.remoting.Manager.invokeAction(

'ApexRemoteInvokeManagerController.saveAccountWithContact',
            contactJson,
            accountJson,
            function(result, event) {
                $rootScope.$apply(function() {
                  if (event.status) {
                    deferred.resolve(result);
                  } else {
                    deferred.reject(event);
                  }
                })
            },
            { buffer: true, escape: true, timeout: 30000 }
        );

        return deferred.promise;
    }

}]);


 accountApp.controller('MainController',function($scope,
'accountAndContactsFactory',function($scope,   
accountAndContactsFactory))
 {

  $scope.save = function(contact,account){

accountAndContactsFactory.save(contact,account).then(function(result) 
               {$scope.message = "success";},
         function(error) {$scope.message = "wrong";}
          )
    }
 });

 </script>  
 </head>
 <body>  
<div class="bootstrap" ng-app="accountApp" ng-   
controller="MainController">
<form name="simpleForm" novalidate="novalidate">
    <table>
    <tr>
        <td>First Name:</td><td><input type="text" name="FirstName" ng-  
model="contact.FirstName"/></td>
    </tr>
    <tr>
        <td>Last Name:</td><td><input type="text" required="true"   
name="LastName" ng-model="contact.LastName"/>
        <span class="error" ng-  
show="simpleForm.LastName.$error.required">Required!</span></td>
    </tr>
    <tr>
        <td>Business Name:</td><td><input type="text" required="true"   
name="BusinessName" ng-model="account.Name"/>
    <span class="error" ng-
show="simpleForm.BusinessName.$error.required">Required!</span></td>
    </tr>
     <tr>
       {{message}}
    </tr>
    <tr>
       <input type="button" ng-click="save(contact,account)" ng-  
disabled="simpleForm.$pristine || simpleForm.$dirty &&   
simpleForm.$invalid"     value="Save"/>
      </tr>
      </table>

      </form>
   </div>
   </body>
   </html>
   </apex:page>

My Apex Controller

global class ApexRemoteInvokeManagerController {

public ApexRemoteInvokeManagerController() {

}

public ApexRemoteInvokeManagerController(ApexPages.StandardController 
controller)
{

}

@RemoteAction
global static void saveAccountWithContact(String contactJson,String 
accountJson)
{
   Account a = (Account)JSON.deserialize(accountJson,Account.class);
   insert a;
   Contact c = (Contact)JSON.deserialize(contactJson, Contact.class);
   c.AccountId = a.Id;
   upsert c;
 }

}
1
  • inspect your JS console and paste the error . Jan 13, 2016 at 13:55

1 Answer 1

1
app.controller("ContactCtrl", function($scope,appFactory) {

   $scope.contacts = [];
      $scope.getContacts = function() {
     Visualforce.remoting.Manager.invokeAction(
         '{!$RemoteAction.SampleRemoteActionPageController.myContacts}', 
     function(result, event) {
           $scope.contacts = result;
         $scope.$apply();
      }); 
    }

      $scope.saveData = function(contact,Account) {
    $scope.message = appFactory.saveAccountWithContact(contact,Account);
    $scope.apply();
 }    
  });

FActory Code

    app.factory('appFactory', function(){
    return {
    saveAccountWithContact: function(contact,Account) {
      var reply ="";
       var jsonContact = JSON.stringify(contact);
       var jsonAccount = JSON.stringify(Account);
       Visualforce.remoting.Manager.invokeAction(

   '{!$RemoteAction.SampleRemoteActionPageController.saveAccountWithContact}',
        jsonContact,jsonAccount,
        function(result, event) {
         if (event.status){
            reply = "Factory Saved Data";
         }else
         {
           reply  = "Failed";
         }
   }); 
   return reply;
 }
};             
 });

I understood the meaning of factory and thus the answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .