【问题标题】:Angularjs reload controller when click on href单击href时Angularjs重新加载控制器
【发布时间】:2016-05-16 12:56:52
【问题描述】:

我是 Angular 的初学者! 请帮帮我! 有一个 webapp(本地目录浏览器)。 我有 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);
    });

和 index.cshtml 中的 html:

<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>

当我第一次运行应用程序时,它会成功运行,我的 model 在我的 app.js($scope.model = response.data) 和 index.cshtml 中我有我的列表目录、文件和驱动器...

怎么办??? 当我点击 {{dir.Name}} 时,我想用 currentPath 的新值重新加载 app.js 控制器(或 getDirectoryBrowseModel(currentPath)),例如:currentPath = "http://localhost:9298/api/browse?path=C:\";。我想在视图 index.cshtml 中查看新的所有目录和文件。 谢谢!!!

【问题讨论】:

    标签: jquery angularjs asp.net-mvc


    【解决方案1】:

    您可以在控制器中使用$window,这将使用新网址重新加载页面

    <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'
    }
    

    【讨论】:

      【解决方案2】:

      如果我理解您的问题,答案可能是使用 angular-ui-router 更改状态 https://github.com/angular-ui/ui-router

      【讨论】:

        【解决方案3】:

        var currentPath = "http://localhost:9298/api/browse"; - 它是某些服务上的 API 路径,我将它发送到我的控制器 函数getDirectoryBrowseModel(路径){ $http.get(path, { responseType: "JSON" }) ... 我想在 getDirectoryBrowseModel(path) 中更改我的参数路径并在视图中刷新我的数据(index.cshtml) - 结果:视图中的新文件和目录

        【讨论】:

          【解决方案4】:

          user2954587 的回答帮助我做出了正确的决定。

          正确的决定是:将函数$scope.clickToPath添加到app.js,然后添加 ng-click='clickToPath("http://localhost:9298/api/browse?path=" + dir.Path)'index.html.

          中的 a href

          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);
              }
          });
          

          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>
          

          谢谢大家

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-02-03
            • 1970-01-01
            • 1970-01-01
            • 2014-07-17
            • 2018-04-30
            • 1970-01-01
            • 2015-12-17
            相关资源
            最近更新 更多