【问题标题】:Unable to bind $scope value to view on Url change无法绑定 $scope 值以查看 Url 更改
【发布时间】:2017-05-13 11:14:02
【问题描述】:

我尝试使用 angularJS 创建搜索应用程序。我遇到了绑定 $scope 值以查看路由器 URL 更改时的问题。

我在 /main 中有一个搜索字段。当我编写查询并单击搜索按钮时,该函数会获取数据并将其分配给范围变量。路由器 URL 将更改为“/结果”和相应的视图已显示。但视图没有绑定范围值。 /main 和 /Result 使用相同的控制器。

主模块中的路由器代码:

 $routeProvider.
                      when('/main', {
                          templateUrl: '/views/search.html',
                          controller: 'searchCtrl'


                            }).when('/Result',{                                 
                                templateUrl:'/views/Result.html',
                                  controller: 'searchCtrl' ,
                                  reloadOnSearch: false


                                }).otherwise({      

                          redirectTo: '/main'
                      });

控制器: 从 /main 中单击按钮

$scope.fetchResult=function(searchWord){

 shService.fetchResultDocumentsList(searchWord).data.then(function(response){
//service call here-data fetch is successfull.
 $scope.docResultList=response[0];
                 $scope.docResultList=response[0];
                 $scope.documents =  $scope.docResultList.data.documentEntities;
$location.path('/Result');
}

当相应的视图发生变化时,绑定没有完成。但是当我用 $rootScope.documents 替换 $scope.documents 时,绑定是成功的。 我已阅读 $rootScope 鼓励使用 $scope。

【问题讨论】:

    标签: angularjs angular-routing


    【解决方案1】:

    当您从一个页面移动到另一个页面时,控制器和 $scope 会重新初始化。如果你想使用 $scope ,你应该考虑使用 service 在控制器之间共享数据。

    创建一个服务,它将保存您的变量。

    angular.service("dataService", function() {
        this.value1 = ""   
    });
    

    在您的控制器中引用该服务,

    angular.controller("myCntrl1", function($scope, dataService) {
        $scope.val= dataService.value1 ;
    });
    
    
    angular.controller("myCntrl2", function($scope, dataService) {
        $scope.val= dataService.value1 ;
    });
    

    【讨论】:

    • /main 和 /Result 使用相同的控制器,因为它使用了一个通用函数(我上面提到的函数)。
    • @stars 即使​​你使用相同的控制器,它也会在你的路线改变时重新初始化
    【解决方案2】:

    正如 Sajeetharan 所说,当您更新位置时,$scope 会重新初始化。

    使用 angularJs,您无需更改位置。您只需在用于搜索的同一视图中更新 $scope。 但是如果你真的需要使用另一个视图,并且假设你的搜索只返回字符串,你可以尝试通过 url 传递数据,然后从你的控制器中获取它。

    类似这样的东西(未测试):

    解决方案 1(角度方式)

    控制器

    $scope.documents = ""; // init your results to empty/null, as you need
    
    $scope.fetchResult=function(searchWord){
    
     shService.fetchResultDocumentsList(searchWord).data.then(function(response){
    //service call here-data fetch is successfull.
     $scope.docResultList=response[0];
                     $scope.docResultList=response[0];
                     $scope.documents =  $scope.docResultList.data.documentEntities;
    $location.path('/Result');
    }
    

    查看

    <!-- search div is over here -->
    
    <!-- results goes here -->
    <div ng-if="$scope.documents">
     {{$scope.documents}}
    </div>
    

    解决方案 2(您的方式)

    路由器

    $routeProvider.
    
    when('/main', {
    templateUrl: '/views/search.html',
    controller: 'searchCtrl'})
    
    .when('/Result/{data:[a-z]{1,}}',{ //here we specify that data expected format is only lowercase letters
     templateUrl:'/views/Result.html',
     controller: 'searchCtrl' ,
     reloadOnSearch: false
    })
    .otherwise({      
     redirectTo: '/main'
    });
    

    控制器

    //  dont forget to inject $stateParams service in your controller
    if( !$stateParams.data){
     $scope.data = $stateParams.data;
    }
    

    【讨论】:

    • 我需要使用不同的视图,因为 /main 中使用的视图仅用于搜索输入,结果显示在与 /Result 关联的相应视图中
    猜你喜欢
    • 2021-08-14
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多