1

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 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

excellent... and it's very helpful, I don't know about params for state.
0

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

I need to be able to set the test variable from the app.js. Is that possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.