I am working on an angular application. I have one controller and one view. I would like to use the same view with and without a search field. That means if the user needs to see the search view or the regular view with the data already populated, then I would like to do this with the same controller and view. My thought that was that I could maybe pass an additional parameter from the app.js to decide in the controller if the search field needs to be displayed or not. Is that possible?
2 Answers
I found the solution set the params property to an object:
In the app.js:
.state('inventorySearchReport', {
url: "/inventoryReport",
templateUrl: "templates/reports/inventoryReport.html",
controller: "inventoryReportCtrl",
cache: false,
params: {
search: { value: true }
}
})
.state('inventoryReport', {
url: "/inventoryReport",
templateUrl: "templates/reports/inventoryReport.html",
controller: "inventoryReportCtrl",
cache: false,
params: {
search: { value: false }
}
})
In your controller inject $state and use as:
$scope.enableSearch = $state.params.search
1 Comment
Joao Polo
excellent... and it's very helpful, I don't know about params for state.
You can use ng-if Angular documentation for ng-if
First initialize a variable say
$scope.test= false; in your controller
suppose you want to display the search field when your data is populated through $http then in that you change
{
$scope.test = true;
$http({..})
}
and in your view
<input type="text" name="search" ng-if="test==true"/> // will display the search when data is populated
1 Comment
Sonu Kapoor
I need to be able to set the test variable from the app.js. Is that possible?