D
D
Dmitry Arushanov2016-02-23 17:05:14
JavaScript
Dmitry Arushanov, 2016-02-23 17:05:14

How to access an object in an angular directive?

Good day to all.
The topic of the question is a little clumsy .. But I will try to state the essence of the issue.
There is an application in angular. And there is a cloudinary service with their widget cloudinary.com/documentation/upload_widget
For cloudinary there is an angular wrapper from cloudinary themselves - but it is not complete .. for example, there is no widget there. etc.
Therefore, I decided to do it myself ... from the installation description to the widget
1) add a file to the page

<script src=" http://widget.cloudinary.com/global/all.js" type="text/javascript"></script>

2) and then you can freely use the widget
cloudinary.openUploadWidget({ cloud_name: 'demo', upload_preset: 'a5vxnzbp'}, 
      function(error, result) { console.log(error, result) });

  }, false);

and that's it))
Now I'm trying to make a directive... I
put us a page of js plug-ins from cloudinary
<script src=" http://widget.cloudinary.com/global/all.js" type="text/javascript"></script>

and then the directive itself
.directive(
    'cloudinaryFileUpload', [function () {
      return {
        restrict: 'A',
        require: '?ngModel',
        link: function (scope, element, attrs, ngModel) {
          if (!angular.isDefined(ngModel)) {
            return;
          }

          element.on(
            'click', function () {
              cloudinary.openUploadWidget(
               {upload_preset: '******'},
               function (error, result) {
               console.log(error, result)
               }
               );

            }
          );
        }
      }
    }]
  )

When the button is clicked, it throws an error

cloudinary.openUploadWidget - is not a function

But if you just insert the code
cloudinary.openUploadWidget(...);
Then the widget is shown ....
What could be my problem, I can’t imagine ..

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MNB, 2016-02-23
@MNB

trywindow.cloudinary.openUploadWidget

D
Dmitry Arushanov, 2016-02-24
@daruwanov

MNB Thanks for the reply... but that wasn't the problem... I forgot to disable the jquery cloudinary library. Which was used earlier .. and the cloudinary object is also given out there. And they interrupted each other .. And therefore, when I simply inserted a call to the widget on the page immediately after connecting the script with the widget, then it was called - and then there was an interruption to jquery and the wrong object was called in the directive.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question