bootstrap navbar search in other jade file - angularjs

in my Angular JS application, in view folder, in layout.jade file I use bootstrap "nav.navbar.navbar-inverse" from "http://www.tutorialrepublic.com/twitter-bootstrap-tutorial/bootstrap-navbar.php" and I did one change in the following line.
input.form-control(type='text', placeholder='Search')
to
input.form-control(type='text', placeholder='Search' ng-model='searchText')
Then, I use another jade file vieworder.jade, here is the content
extends layout
block content
table.table.table-striped(ng-app="vieworder", ng-controller="viewordercontroller" border='1', cellpadding='7', cellspacing='7')
thead
tr
th(width="10%") Customer Name
th(width="10%") Order Type
th(width="10%") Orders
th(width="10%") Order Date
th(width="10%") Order Status
td(width="10%")
tbody
tr(ng-repeat='Order in Orders | filter:layout.searchText')
td {{ Order.custname }}
td {{ Order.ordertype }}
td {{ Order.noorder }}
td {{ Order.orderdate }}
td {{ Order.orderdate }}
td
a.btn.btn-small.btn-primary(ng-click='cancelOrder(Order.id)') cancel
Now, when I run, I got all the order list in my vieworder page, which is working perfectly. Now I want to apply filter to the table content. For that I use 'searchText' ng-model. But when I type any alphabet to the search field, filter is not working. Please help me..
Here is the controller code
var order = angular.module('vieworder', []);
order.controller('viewordercontroller', function ($scope, $http, $window){
$http.get('/viewalloders').
success(function (data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
$scope.Orders = data;
console.log(data);
}).
error(function (data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("error");
});
console.log("santanu");
$scope.cancelOrder = function (id) {
var path = '/deleteorder/' + id;
var input = [];
input.id = id;
$http.put(path, input).success(function (response) {
console.log("success"); // Getting Success Response in Callback
}).
error(function (response) {
console.log("Santanu :error"); // Getting Error Response in Callback
});
}
});

If you are using same controller for both the views, then you don't need to use layout.searchText. Simply, use searchText since $scope is shared across entire controller. You can directly access it.
If you are using a different controller for both the views, then $scope is different for both the views. In that case you can define your searchText something like:
In your Navbar controller:
$scope.object = {};
$scope.object.SearchText = "someVal";
and then use this as "Order in Orders | filter:'object.searchText'" in your viewOrderController.
Let me know if this doesn't work
Thanks

Related

Where should I view Console.log result in AngularJS?

I am having the following code:
var vocabularies = new MyDataService.myService();
vocabularies.success(function (data) {
console.log(data);
}).error(function (data) {
console.log(data);
});
Where can I view the value of data instead of debugging the above code?
If you want to view the data present in the console then you may use keyboard shortcut Ctrl + Shift + J to bring focus on the console.
If you want to see your data during execution you may use alert instead of console.log.
If you want to bind your data to the HTML you need to use
$scope.variable=data;
you can then get your value by using expression tag in HTML.
{{variable}}
You can assign your data to a $scope variable. And on template you can view it. Find below code:
var vocabularies = new MyDataService.myService();
vocabularies.success(function (data) {
console.log(data);
$scope.viewData = data;
}).error(function (data) {
console.log(data);
$scope.viewData = data;
});
And then on the template:
{{viewData | json}}
The above will print it in json format if its json else normal text

Passing the URL/route parameter from laravel and using in angular

I am new to Laravel 5 and angular.
I am using Laravel routing for traversal and backend operations and angular for just UI operations like fetching data and binding UI grid, etc.
I have following route defined in routes.php file below
routes.php
Route::pattern('clientid', '[0-9]+');
//used for AJAX call from angularjs and populating ui-grid
Route::get('getclients/{clientid?}', 'ClientController#getClients');
//used for displaying Laravel view with ui-grid
Route::get('client/{clientid?}', 'ClientController#showClients');
Please find the angular files:
app.js
var appClients = angular.module('getclients', ['clientsService', 'ui.grid', 'ui.grid.exporter', 'ui.grid.selection']);
clientController.js
appClients.controller('ClientsController', ['$scope', '$http', 'Client', '$interval', '$q', function ($scope, $http, Client, $interval, $q) {
/* Defining UI grid options*/
.
.
/* Calling service to fill the grid*/
Client.get(clientid)
.success(function (data, status, headers, config) {
if (data.length > 0) {
$scope.gridOptions.data = data;
}
});
}
clientsService.js
angular.module('clientsService', [])
.service('Client', function ($http) {
return {
// Get all the photos
get: function (clientid) {
if (clientid !== '') {
return $http.get('/myproject/public/getclients/' + clientid);
}
else {
return $http.get('/myproject/public/getclients/');
}
}
}
});
/*
**Note:**
Have already defined route in routes.php for using the same above:
Route::get('getclients/{clientid?}', 'ClientController#getClients');
*/
EXAMPLE:
Step 1:
Say I am hitting URL: http://<domain>/public/myproject/client/2
The following route would catch it and redirect to view where the ui-grid is present
Route::get('client/{clientid?}', 'ClientController#showClients');
Step 2:
Now, somehow need to figure out how to pass that **2** to angular so that I could pass that parameter while making ajax call and get grid data
I am confused as to how we could use the the url parameter from Laravel in angular?
I reckon that I am missing some concept or doing something wrong here.
Could anyone help me out?
Just a workaround to make it work with angular and without jquery.
From routes.php, the control is transferred to showClients action in ClientsController.php
ClientsController.php (Laravel Controller):
Passed the variable to Laravel view from controller using following statement:
public function showClients($clientid = '') {
return view('masters.clients', compact('clientid'));
}
Clients.php (Laravel View)
Added clientidmodel as ng-model and initialized it with passed clientid from Laravel controller using ng-init
<div ng-app="">
<div ng-controller="ClientsController">
<input type="text" name="txtClientId" ng-model="clientidmodel" style="display: none;" ng-init="clientidmodel = '{!!$clientid!!}'"/>
</div>
</div>
clientController.js
Added the watch to the angular model so that we can capture the initial value passed.
$scope.$watch("clientidmodel", function () {
Client.get($scope.clientidmodel)
.success(function (data, status, headers, config) {
if (data.length > 0) {
$scope.gridOptions.data = data;
}
});
});
Not sure whether this is the efficient way but as of now got the things working with this workaround.
Please let me know in case of any better way to approach the same.
You can achieve this in jquery by
var pathname = window.location.href;
var lastItem = pathname.split("/").pop(-1);
Note : Here you will get the last element
i.e.,
If your url is like yourapp.com/app#/product/15 then the script will return 15. That's the last element after / . You can change this according to your wish.
Then you can pass the value directly inside your Laravel Controller.
$.ajax({
type: "POST",
dataType: 'text',
crossOrigin : true,
data: {param : lastItem},
url: serviceUrl+'/getReceipeDetails',
})
.done(function( data ) {
var result = jQuery.parseJSON(data);
if(result.success==1)
{
$(".yourresult").html('Controller return success');
}
else
{
$(".yourresult").html('Controller return failure');
}
})
.fail( function(xhr, textStatus, errorThrown) {
console.log(errorThrown);
});

Use a custom filter with a controller

I've created a filter that checks the user id and then filters out all results in a ng-repeat that don't have that user id.
If I place my filter,
UserService.getCurrentUser()
.then(function(user_id){
$scope.user_id = user_id;
});
In my controller it works fine, but I want to keep my controller as clean as possible so I want to move it out.
I created a file called "userFilter.js" and this is the code,
angular.module('addMovieseat', [])
.filter('user_filter', function() {
UserService.getCurrentUser()
.then(function(user_id){
$scope.user_id = user_id;
});
});
But when I inject the filter into my controller I get an error,
Error: [$injector:unpr] Unknown provider: user_filterProvider <- user_filter <- addMovieCtrl
Also this is my service where I get my current user id,
(function(){
"use strict";
angular.module('addMovieseat')
.factory('UserService', function($http, $q){
return{
getCurrentUser: function(){
var user_id = null,
deferred = $q.defer();
// Get the current user id and use it to filter out all movies that do not correspond to that id in the main overview.
$http.get(('/users.json'),{ cache: true}).
success(function (data, status, headers, config) {
if (status == 200) {
deferred.resolve(data.id);
} else {
deferred.resolve(false);
console.error('Error happened while getting the user list.')
}
});
return deferred.promise;
}
}
});
})();
It sounds as if you might be better served using ng-if on your repeated items. Like this
<li ng-repeat="movie in movies" ng-if="movie.user_id == user_id"></li>
Assuming $scope.user_id in your controller is the id being checked for, and your list of movies is an array. Like this:
$scope.user_id = 'Tim';
$scope.movies = [
{name: 'Movie Title 1', user_id: 'Tim'},
{name: 'Movie Title 2', user_id: 'Jane'},
{name: 'Movie Title 3', user_id: 'Bob'}
];
This will only render the movie with user_id 'Tim'.
EDIT: Documentation for ng-if
EDIT 2: Based on the comment by OP, updated code to reflect specific issue.
i actually think what your are looking for is
<li ng-repeat="movie in movies|filter:{user_id:user:id}:strict"></li>
as shown here
https://docs.angularjs.org/api/ng/filter/filter
using ng-if might lead to weird behavior under certain circunstances make sure you read https://docs.angularjs.org/api/ng/directive/ngIf

Angular.js - Jade, Form Submission

I'm new to Angular and I'm building my first realtime page based on the boiler plate from:
https://github.com/jimakker/angular-express-bootstrap-seed
I'm not able to get my first form to submit. In fact, nothing happens when I click the button. Given below are the relevant snippets from the code:
Partial View:
.row
form(ng-controller='RegisterCtrl')
#accordion.panel-group
//- Panel 1
.panel.panel-default
.panel-heading
h4(class='panel-title')
a(data-target='#collapse1', data-toggle="collapse", data-parent="#accordion", class="left-m-1em cursor-pointer") Basic Information
#collapse1.panel-collapse.collapse
.panel-body
.row
.col-lg-2(ng-controller='DropdownCtrl')
select(id="gender", ng-model='register.gender', ng-init="getValues('gender')", ng-options='r.value for r in results track by r.key', chosen, class="form-control", data-placeholder="Gender", data-toggle="tooltip", data-trigger="hover", data-placement="top", title="Gender")
option(value="")
//- More panels in between and finally, Panel 6
.panel.panel-default
.panel-heading
h4(class='panel-title')
a(data-target='#collapse6', data-toggle="collapse", data-parent="#accordion", class="left-m-1em cursor-pointer") Family
#collapse6.panel-collapse.collapse
.panel-body
.row
.col-lg-3.col-lg-offset-5
button(type="button", class="btn btn-success btn-lg", ng-click="saveProfile")
span(class="glyphicon glyphicon-ok-circle")
| Submit
I looked at the rendered output and confirmed that the Submit button is indeed within the form tag.
Controller:
function RegisterCtrl($scope, $http) {
$scope.register = {};
$scope.saveProfile = function(item, event) {
alert("called this!");
var json = {
gender: $scope.register.gender,
marital_status: $scope.register.marital_status,
dob: $scope.register.dob,
height: $scope.register.height,
weight: $scope.register.weight,
complexion: $scope.register.complexion,
health: $scope.register.health
};
var responsePromise = $http.post("/api/register", json, {});
responsePromise.success(function(dataFromServer, status, headers, config) {
console.log(dataFromServer.title);
});
responsePromise.error(function(data, status, headers, config) {
alert("Submitting form failed!");
});
};
}
RegisterCtrl.$inject = ['$scope', '$http'];
I'm not even hitting the first alert statement and I'm totally unsure why. The other controller DropdownCtrl (used for individual form elements) works just fine and I'm able to populate the values dynamically using the controller. Please help me find the missing piece.
The syntax is ng-click="saveProfile()" rather than ng-click="saveProfile"
Note also that this won't pass any arguments to the function, if you want to do that then you need to pass them inside the markup too.
Found the problem. Had some stray code in the controller file that read:
` function MyCtrl2() {
}
MyCtrl2.$inject = [];
function RegisterCtrl() {
}
RegisterCtrl.inject = [];`
And since this was in the file after the actual definition, it messed up the functionality.

Can you bind data from one scope to update when another changes in angularJS

My web app depends on one specific variable changing throughout the user's visit. It controls what data the user will see at any given time, essentially akin to a TAG.
If the $scope.tagid = 1, is it possible to have another angular model to instantly update its own dataset when tagid is changed to $scop.tagid = 2?
<script >
function PageCtrl($scope) {
$scope.text = '<?=$tagid?>';
}
$scope.showThread = function(tagid) {
$http({method: 'GET', url: 'api/example/thread/id/' + tagid}).
success(function(data, status, headers, config) {
$scope.appDetail = data; //set view model
$scope.view = './Partials/detail.html'; //set to detail view
}).
error(function(data, status, headers, config) {
$scope.appDetail = data || "Request failed";
$scope.status = status;
$scope.view = './Partials/detail.html';
});
}
</script>
<div ng-controller="PageCtrl">
<input ng-model='text' />
<ul>
<li >
<span>{{text}}</span>
</li>
</ul>
</div>
Above is the skeleton of what i'm looking to do.
I realize that if I wanted to, I could call showThread() after each user action and update the data...however, because of the awy I'm looking to set up the site, It makes more sense to only change the tagid, then have everything else update immediately after, rather than picking and choosing each part of the site I want to update. i.e. there may, in addition to showThread(), be updateHeader(), changeSidebar() etc.
Thanks!
I have personally had success using a service; **Assuming that you are using 2 controllers on 1 page, I would create a service like this:
MyApp.app.service("tagDataSvc", function () {
var _tagId = {};
return {
getTagId: function () {
return _tagId;
},
setTagId: function (value) {
_tagId = value;
}
};
});
Next, inject this service into the controllers where this will be used.
In your main controller where you are controlling the TagId (PageCtrl), you would need to set the shared tagId value with a call to the service: tagDataSvc.setTagId($scope.text) You can do this explicitly, or add a $watch on $scope.text, or whatever you prefer.
Finally, in the second controller that you want to automagically update, add a $watch on this service's getTagId() function like so:
$scope.$watch(function () { return tagDataSvc.getTagId(); }, function (newValue, oldValue) {
if (newValue != null) {
$scope.tagId2 = newValue;
//reload whatever needs updating here
}
}, true);

Resources