2

I know two ways to declare and define controllers in angularjs:

1st way:

myApp.controller('myController', ['$scope', '$uibModal', myController]);

function myController($scope, $uibModal, myObject){
    $scope.params = {};
    $scope.open = function(){
       $uibModal.open({
          templateUrl:'my_dialig.html',
          controller:myDialogController,
          resolve:{
              myObject:function(){
                 return $scope.params;
              }
          }
      });
    }
}

2nd way:

myApp.controller('myController', function($scope, $uibModal){
   $scope.params = {};
   $scope.open = function(){
     $uibModal.open({
        templateUrl:'my_dialog.html',
        controller:myDialogController,
        resolve:{
          myObject:function(){
               return $scope.params;
          }
        }
     });
   }
});

Where I use myObject:

...

myApp.controller('myDialogController', function($uibModalInstance, myObject){
    console.log(myObject);
}
...

this is UI Bootstrap Modal Dialog code, and the object returned from resolve is the data to pass to the dialog scope.

But the problem comes:

the 1st way:

cannot be allowed by AngularJS, because it cannot find the definition of myObject in myDialogController.

the 2nd way:

the value printed in log: 'undefined'.

Is the way I declare and define the controller wrong?(the other parts of the controller code work just ok,though), or is the way myObject passed to controller wrong?(the code to use the controller is from AngularJS UI Bootstrap sample code: “https://angular-ui.github.io/bootstrap/”).

The mistake I make may seem silly, but it matters to me, thanks for poninting it out!

7
  • Where do you have the resolve? Commented Apr 11, 2016 at 1:58
  • @Joy , in angular-ui.github.io/bootstrap, Modal part, it says:resolve (Type: Object) - Members that will be resolved and passed to the controller as locals; it is equivalent of the resolve property in the router. Commented Apr 11, 2016 at 2:37
  • I would suggest to make it alive on JSFiddle/Plunker/Pen Commented Apr 11, 2016 at 2:58
  • see: plnkr.co/edit/JFSFHqhMScyBhvvPHMll?p=info Commented Apr 11, 2016 at 3:06
  • But the code is working properly, am I right? I can open the modal, display three items, select and close. So, what is wrong? Commented Apr 11, 2016 at 3:31

3 Answers 3

1

You should create a service or constant to inject into a controller, a constant if your object is constant (duh) and a service if it contains data you need dynamically (functions as values).

so...

myApp.service('myObject', function(){
    return {
        function(){
            ...
        },
        ....
    }
})

or

myApp.constant('myObject', {
   'someKey': 'someValue',
   ...
})
Sign up to request clarification or add additional context in comments.

6 Comments

the code should work by pass data through 'resolve', see code in; angular-ui.github.io/bootstrap
you're passing in a function as myObject, not an object, so maybe something like: myObject: { getParams: function(){ return $scope.params} }
If I use 'myObject()' , it logs 'myObject is not a function'
Is that if you do what I suggested in my previous comment?
use your code, still logs 'undefined', I think I should figure how 'resolve' works in $uiModal
|
0

looks like you just want to pass $scope.params to dialog controller. if this is the case you don't need to inject myObject to myController.

myApp.controller('myController', function($uibModal){
   $scope.params = {};
   $scope.open = function(){
     $uibModal.open({
        templateUrl:'my_dialog.html',
        controller:myDialogController,
        resolve:{
          myObject:function(){
             return $scope.params;
          }
        }
     });
   }
});

2 Comments

I've improved myedits,(the use of myObject is in myDialogController)
are you still getting undefined when you open the dialog?
0

What is the myObject. Angular injector can only inject the object in angular(like myController, defined by app.controller). So you should turn your myObject to angular object, service or factory is ok.

myApp.service("myObject", function() {
    ...
})

If you want to inject some object by yourself, you can use the $controller like this:

$controller("myController", {myObject: myObject});

here is the documentation: $controller

2 Comments

I can use factory or service, but I think the design is not good, in fact the data passed to dialog, is just selected item id. And the code should work fine like sample code of 'Modal' part in: angular-ui.github.io/bootstrap
Answer updated. UI Bootstrap inject the instance through the $controller.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.