I am trying to setup a Laravel and Angular application. I wanted to place my work files under resources/assets/js and then use webpack and elixir to compile those code and create a single app.js file in my public directory. But I am not sure how to do that? Can anyone guide me with a step based approach for it?
Here is a gulp only process
No webpack used. Please anyone can rewrite this with Laravel Mix I would really appreciate.
1- Install npm modules
`npm i --S bower gulp gulp-concat gulp-uglify`
2- Install Angular with bower
bower install --save angular angular-sanitize angular-ui-router
3- Now that you have your angular assets in bower_components, create a file at the root of your project to load all vendors. Let's create /vendor.json
[
"bower_components/angular/angular.min.js",
"bower_components/angular-sanitize/angular-sanitize.min.js",
"bower_components/angular-route/angular-route.min.js"
]
Add all your vendors to that file. Bower or Npm vendors. Anything you download that is not part of your code.
4- Go to /gulpfile.js and add a task
var gulp = require('gulp),
concat = require('gulp-concat'),
uglify = require('gulp-uglify');
gulp.task('vendorjs', function() {
var source = require('./vendorjs.json');
return gulp.src(source)
.pipe(concat('vendors.min.js'))
.pipe(uglify())
.pipe(gulp.dest('public/assets/js'))
});
That task will compile all vendors assets to /public/assets/js/vendors.min.js
5- In resources/assets/js, create following directories controllers, modules and the file app.js. Inside of app.js do
// 'resources/assets/js/app.js'
(function(){
angular
.module('myApp', ['app.core', 'app.controllers']);
})();
6- In the modules directory create core/module.js. This is the core module where you load all external modules loaded via bower or npm.
// 'resources/assets/js/modules/core/module.js'
(function(){
angular
.module('app.core', ['ngRoute', 'ngSanitize']);
})();
7- Still in the modules directory create controllers/module.js. All your controllers will be bound to this module.
// 'resources/assets/js/modules/controllers/module.js'
(function(){
angular
.module('app.controllers', []);
})();
8- Now you can write your controllers like this
// 'resources/assets/js/controllers/home.js'
(function(){
angular
.module('app.controllers')
.controller('HomeController', Controller);
//Use injection for assets minification
HomeController.$inject = ['$scope', '$http'];
function HomeController($scope, $http)
{
var vm = this;
activate();
function activate()
{
vm.sayHi = function() {
console.log('Hi');
}
}
}
})();
9- If you want to define routes
// 'resources/assets/js/modules/routes/routes.js'
(function(){
angular
.module('app.routes', [])
.config(routesConfig);
//Use injection for assets minification
routesConfig.$inject = ['$stateProvider', '$locationProvider', '$urlRouterProvider'];
function routesConfig($stateProvider, $locationProvider, $urlRouterProvider)
{
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url: '/',
templateUrl: '/templates/home.html',
controller: 'HomeController'
})
...
}
})();
10 - Create angular task in gulpfile
gulp.task('angular', function() {
var root = 'resources/assets/js';
var source = [
root + '/app.js',
root + '/modules/**/*module.js',
root + '/controllers/**/*js'
];
return gulp.src(source)
.pipe(concat('app.min.js'))
.pipe(uglify()) //comment this line when in development
.pipe(gulp.dest('public/assets/js'))
});
I think that's it. I may have made one or 2 typos, but angular that's the gist of it. How I use gulp with laravel for angular
I couldn't do this from scratch but there is a package that uses lumen, angular2. Providing a link for the same so anyone with the same problem can be benefited. Link: anvel.io
Related
Hi I have problem with install this package :
https://github.com/marceljuenemann/angular-drag-and-drop-lists
in index.ejs I include
<script src="/javascripts/angular-drag-and-drop-lists.js"></script>
He said
Add the dndLists module as a dependency to your angular app. Then I add:
var App = angular.module('TodoListApp', [ dndLists ]);
App.controller('TodoListCtrl', function ($scope, $http, ApiService) {
And result is
https://imgur.com/a/be4a4
What I did wrong?
Recently I bought a fuse AngularJS theme from http://themeforest.net/item/fuse-angularjs-material-design-admin-template/12931855 but what kind of AngularJS architecture does this code follow? All Files Are Separate modules, controller and HTML file, and it uses gulp to run
This is Module
(function ()
{
'use strict';
angular
.module('app.components.tables.datatable', [])
.config(config);
/** #ngInject */
function config($stateProvider)
{
$stateProvider.state('app.components_tables_datatable', {
url : '/components/table/datatable',
views : {
'content#app': {
templateUrl: 'app/main/components/tables/datatable/datatable.html',
controller : 'DatatableController as vm'
}
},
resolve: {
Employees: function (apiResolver)
{
return apiResolver.resolve('tables.employees100#get');
}
}
});
}
})();
This is Controller
(function ()
{
'use strict';
angular
.module('app.components.tables.datatable')
.controller('DatatableController', DatatableController);
/** #ngInject */
function DatatableController(Employees)
{
var vm = this;
// Data
vm.employees = Employees.data;
vm.dtOptions = {
dom : '<"top"f>rt<"bottom"<"left"<"length"l>><"right"<"info"i><"pagination"p>>>',
pagingType: 'simple',
autoWidth : false,
responsive: true
};
// Methods
//////////
}
})();
Here is a good documentation which helps to start with Fuse theme's angular version.
http://withinpixels.com/themes/fuse/documentation/getting-started/introduction
It uses modular structure under /app directory. To add a new module, simply put your module files into your preferred location inside the app/ directory and then add the module name as a dependency into the app/index.module.js file.
Unzip the zip file that you have downloaded from Themeforest. Inside the zip file, you will find the Skeleton Project (Fuse-1.x.x.zip) along with the Demo Project (Fuse-1.x.x-demo.zip), PSD designs and a readme file. After extracting Fuse-1.x.x.zip or Fuse-1.x.x-demo.zip, you will find /src folder where you need to build your modules under /app folder. After completing you need to run "gulp build" (for production) or "gulp build:dev" (for development). Finally you will get your build theme under /dist folder. Now you can use it in any where. Even you can run using apache server.
Probably a newbie's question, but I need to add the ngDialog module to angular.
I noticed that after installing with bower Yeoman doesn't automatically update files, so I added
<script src="bower_components/ngDialog/js/ngDialog.js"></script>
to index.html.
I went ahead and added 'ngDialog' to the main module dependencies, like this
angular.module('sigaApp', ['ngDialog'])
.controller('MainCtrl', function () {
this.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
});
I also added $scope and 'ngDialog' to my controller, like this
angular.module('sigaApp')
.controller('myNewCtrl', ['$scope', 'ngDialog',
function ($scope, ngDialog) {
$scope.open = function () {
ngDialog.open({ template: 'templateId' });
};
}
]
);
That's ALL I did. Grunt refreshes the page with no error, and the page console shows no error, but the page shows nothing, and I have just no idea why.
Should it load the dependencies automatically, and I shouldn't be adding these injections manually?
Is there another standard way to add the dependencies?
Any help is appreciated. Thanks!
Answering my own question: Removed the module and installed with bower install ng-dialog --save adding --save to the command line.
What the --save parameter does, actually is that it "Save installed packages into the project's bower.json dependencies" (quoting bower help) and as far as I'm aware of, there's where Yeoman takes the dependencies from to update index.html.
I have a problem to run my tests in Webstorm 8, here is my conf file :
Here is my AngularJS controller :
angular.module('monApp').controller('DashboardCtrl', [
'$scope', function ($scope) {
'use strict';
$scope.foo = 'bar';
}
]);
And my test file :
describe('Controller: DashboardCtrl', function () {
'use strict';
var DashboardCtrl,
scope;
beforeEach(module('monApp'));
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
DashboardCtrl = $controller('DashboardCtrl', {
$scope : scope
});
}));
it('dashboard should be defined', function () {
expect(scope.foo).toBeDefined();
expect(scope.foo).toBe('bar');
});
});
These are very simple, I followed steps of many tutorials, first installed node, then install karma with npm install, and when i run karma with karma start my.conf.js the test pass but it shows Empty test suite.
I precise that in my project, I just have 5 files :
angular.js
angular-mocks.js
the controller
test file
conf file
In the config file, i have added these 4 files with :
// list of files / patterns to load in the browser
files: [
'angular.js',
'angular-mocks.js',
'*.js'
],
When I try to throw the test by right-click I get an error telling : describe is not defined.
Thanks.
I tried to restart from scratch and it appears that my instantiation of my app module wasn't fine, this is better :
var app = angular.module('monApp', []);
app.controller('DashboardCtrl', ['$scope', function ($scope) { ....
And the first problem "empty test suite" was due to wrong basePath in the config file. Anyway, thanks for your answers !
Your my.conf.js must contain :
frameworks: ['jasmine']
And Karma need the bridge to Jasmine framework to be installed :
npm install karma-jasmine --save-dev
By default, karma loads all installed plugins which are starting with "karma-".
I am new to ng-route. I have a site called www.foo.com. And I just want to assign the template HTML (fooapi.view.html) and the controller (fooapi.controller.js) to the URLs like
www.foo.com/api?id=1&id=2&id=3
. I wonder if somebody could give some suggestion.Thanks.
First of all, you will need to install ng-route. To do so, you can either use npm or bower for the installation, or a manual way by downloading ng-route javascript file which can be found here
To install using bower
bower install angular-route
To install using npm
npm install angular-route
After, completing those steps, include the file named angular-route.min.js to your index.html, depending on where you have installed the downloaded files.
<script src="/bower_components/angular-route/angular-route.js"></script>
After doing so, include 'ngRoute' to you angular's module
angular.module('ngApp', ['ngRoute'])
Now you will have to create a angular's config module
.config(function ($routeProvider, $locationProvider) {
$routeProvider.when('/foo/:id', {
templateUrl: 'fooapi.view.html'
controller: 'FooCtrl'
});
})
In your fooapi.controller.js
.controller('FooCtrl', function ($routeParams) {
//getting the id from the URL
console.log($routeParams.id);
});