最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use npm module in angular? - Stack Overflow

programmeradmin0浏览0评论

I am trying to use braintree-web npm module with AngularJS since I get errors when I try and include it in the template with:

<script src=".js"></script>

I have a state called billing that I use to route to my template with the controller, 'BillingController'. I want to be able to inject braintree-web and myscript.js:

<script>
  braintree.setup(
          // Replace this with a client token from your server
          clientToken,
          "dropin", {
            container: "payment-form",
            form: "checkout",
          });
</script>

Please help. how can I do this?

EDIT:

Currently, this is placed a the bottom of my

<!-- braintree sdk -->
    <script src=".js"></script>

    <!-- braintree setup -->
    <script>
      var clientToken = removed;
      braintree.setup(
          // Replace this with a client token from your server
          clientToken,
          "dropin", {
            container: "payment-form",
            form: "checkout",
          });
    </script>

These are the errors I'm getting:

Looks to me like the braintree script is not loading(?)

Thanks for the help

I am trying to use braintree-web npm module with AngularJS since I get errors when I try and include it in the template with:

<script src="https://js.braintreegateway./v2/braintree.js"></script>

I have a state called billing that I use to route to my template with the controller, 'BillingController'. I want to be able to inject braintree-web and myscript.js:

<script>
  braintree.setup(
          // Replace this with a client token from your server
          clientToken,
          "dropin", {
            container: "payment-form",
            form: "checkout",
          });
</script>

Please help. how can I do this?

EDIT:

Currently, this is placed a the bottom of my

<!-- braintree sdk -->
    <script src="https://js.braintreegateway./v2/braintree.js"></script>

    <!-- braintree setup -->
    <script>
      var clientToken = removed;
      braintree.setup(
          // Replace this with a client token from your server
          clientToken,
          "dropin", {
            container: "payment-form",
            form: "checkout",
          });
    </script>

These are the errors I'm getting:

Looks to me like the braintree script is not loading(?)

Thanks for the help

Share Improve this question edited Dec 17, 2015 at 19:16 c0de asked Dec 17, 2015 at 8:06 c0dec0de 8192 gold badges9 silver badges21 bronze badges 4
  • Do you have braintree script in the resources? do you have script tag before the above braintree script? – Sudheer Commented Dec 17, 2015 at 8:13
  • What errors are you getting? Please give some more details. – Hinrich Commented Dec 17, 2015 at 8:25
  • @Sudheer yes, see edit. – c0de Commented Dec 17, 2015 at 19:17
  • @Hinrich see attached ss of errors. Drop-in ui is not loading. Styles are not applied. – c0de Commented Dec 17, 2015 at 19:17
Add a ment  | 

4 Answers 4

Reset to default 0

Do you use braintree-web from this url? https://github./jeffcarp/braintree-angular

This is module special for angular. Then you should create file like app.js and paste this code:

var yourApp = angular
  .module('yourApp', ['braintree-angular'])
  .constant('clientTokenPath', '/path-or-url-to-your-client-token');

For example:

(function () {
    'use strict';

    var app = angular.module('yourModuleName', [
        'braintree-angular'
    ]);

    app.constant('clientTokenPath', '/path-or-url-to-your-client-token');

    app.config(['$interpolateProvider', function ($interpolateProvider) {
        $interpolateProvider.startSymbol('[[');
        $interpolateProvider.endSymbol(']]');
    }]);


}());

Your controller.js could be like this:

(function() {
    'use strict';

    angular
            .module('yourModuleName')
            .controller('DashboardCtrl', DashboardCtrl);

    DashboardCtrl.$inject = ['$scope', '$braintree'];

    function DashboardCtrl($scope, $braintree) {

        var client;
        $scope.creditCard = {
            number: '',
            expirationDate: ''
        };

        var startup = function() {
            $braintree.getClientToken().success(function(token) {
                client = new $braintree.api.Client({
                    clientToken: token
                });
            });
        };

        $scope.payButtonClicked = function() {

            // - Validate $scope.creditCard
            // - Make sure client is ready to use

            client.tokenizeCard({
                number: $scope.creditCard.number,
                expirationDate: $scope.creditCard.expirationDate
            }, function (err, nonce) {

                // - Send nonce to your server (e.g. to make a transaction)

            });
        };

        startup();

    }


}());

Notice that app.js should be included before rest of your controllers, factories and services, and after you angular.js and braintree.js library.

To expand on @danday74 we use browserify like this at work.

In a nutshell this is how we use it.

So Browserify if your not aware its a nodejs CLI that lets you use NodeJs style require for client code. It allows you to write well structured, modular client code and build it into a single file to include into your page. Another benefit is every file is scoped to that single file. So no more accidental global's (if your not using strict mode). The only things exposed from your files are things that you explicitly export.

Because its a CLI you to have to install it globally so you can use it on the mand line.

npm install -g browserify

Then to run it simply from the mand line do

browserify main.js > output.js

and include that on your page

<script src="output.js"></script>

We personally alias that in our package.json as we do other things like linting and sass. here's an example

{
  "name": "some app",
  "scripts": {
    "build:js": "browserify src/index.js > dist/built.js",
    "build:css": "node-sass sass/main.scss dist/built.css",
    "build": "npm run build:js && npm run build:css"
  }
}

Now we just run npm run build and it will build out sass and js.

What browserify will do is recursively traverse your file looking for require calls, it will then travel into that file and repeat. As part of its search path it will look in your node_modules folder. This is why you can include modules installed through npm.

Here's a small example

//In a file called data.js located in same folder as main.js
module.exports = [1, 2, 2, 3, 4, 5, 5, 6];


//in a file called main.js
var unique = require('uniq'),
    data = require('./data');

console.log(unique(data)); //[1, 2, 3, 4, 5, 6]

What this will do is first look for a module called uniq installed through NPM (as there is no relative or absolute path). It will then look for our own file called data.js located in the same folder.

When would build this with browserify main.js > out.js

I hope this helps explain what browserify is and how it can be helpful to manage you structure when you wish to include NPM modules in the client. I'm aware this won't be suitable for all especially if you have a large application that doesn't use browserify but now i use a build tool like this to write modular code i would never go bacl.

I think your best hope is browserify. I have never tried it myself but I think the idea behind it is converting NodeJS code into a format the browser can understand.

npm i braintree
npm i browserify

Maybe try a small test and see if it works?

In console do: npm install braintree --save

then in your javascript require('braintree'), now you will have the braintree functions available

发布评论

评论列表(0)

  1. 暂无评论