0

I am beginner in Angular! Help me, please! There is a webapp (local directories browser). I have app.js:

var currentPath = "http://localhost:9298/api/browse";

angular.module("browserApp", [])
    .controller("BrowseController", function ($scope, $http) {
        function getDirectoryBrowseModel(path) {
            $http.get(path, { responseType: "JSON" })
            .then(function (response) {
                $scope.model = response.data
            });
        };
        getDirectoryBrowseModel(currentPath);
    });

and html in index.cshtml:

<div ng-controller="BrowseController">
 <span ng-repeat="drive in model.Drives">
{{drive.Name}}
</span>

<div>{{model.CurrentDrive.Name}}</div>

<div>{{model.CurrentDirectory.Path}}</div>

<div ng-repeat="dir in model.Directories">
    <a href="#" onclick='$state.reload();'>{{dir.Name}}</a>
</div>

<div ng-repeat="file in model.Files">
    {{file.Name}}
</div>

When I run app first time, then It works succesfully and I get my model in my app.js($scope.model = response.data) and in index.cshtml I have my list of directories and files and drives ...

What to do??? When I click on {{dir.Name}} I want to reload app.js controller( or getDirectoryBrowseModel(currentPath)), with new value of currentPath, for example: currentPath = "http://localhost:9298/api/browse?path=C:\";. And I want to see new all directories and files in view index.cshtml. Thanks!!!

4 Answers 4

2

you can use $window from your controller and that will reload the page with a new url

<div ng-repeat="dir in model.Directories">
    <a href="#" ng-click='updateUrl()'>{{dir.Name}}</a>
</div>


# in controller 
updateUrl = function() {
   $window.location.href = 'my/path'
}
Sign up to request clarification or add additional context in comments.

Comments

0

If I understood your question the answer might be to use angular-ui-router for changing the states https://github.com/angular-ui/ui-router

Comments

0

var currentPath = "http://localhost:9298/api/browse"; - It's path to API on some service and I send it to my controller in function getDirectoryBrowseModel(path) { $http.get(path, { responseType: "JSON" }) ... I want to change my parameter path in getDirectoryBrowseModel(path) and refresh my data in view(index.cshtml) - result: new files and directories in view

Comments

0

The answer of user2954587 helped me to make the right decision.

The right decision is: added function $scope.clickToPath to app.js and then added ng-click='clickToPath("http://localhost:9298/api/browse?path=" + dir.Path)' to a href in index.html.

app.js:

var currentPath = "http://localhost:9298/api/browse";

angular.module("browserApp", [])
.controller("BrowseController", function ($scope, $http) {
    function getDirectoryBrowseModel(path) {
        $http.get(path, { responseType: "JSON" })
        .then(function (response) {
            $scope.model = response.data
        });
    };
    getDirectoryBrowseModel(currentPath);

    $scope.clickToPath = function (path) {
        getDirectoryBrowseModel(path);
    }
});

in index.cshtml:

<div ng-controller="BrowseController">
 <span ng-repeat="drive in model.Drives">
{{drive.Name}}
</span>

<div>{{model.CurrentDrive.Name}}</div>

<div>{{model.CurrentDirectory.Path}}</div>

<div ng-repeat="dir in model.Directories">
    <a href="" id="{{dir.Name}}" ng-click='clickToPath("http://localhost:9298/api/browse?path=" + dir.Path)'>{{dir.Name}}</a>
</div>

<div ng-repeat="file in model.Files">
    {{file.Name}}
</div>

Thanks to all

Comments

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.