在angularjs中,最佳实践是将简单的变量值传递给另一个作用域

Best practice pass the simple variable value to another scope in angularjs?

本文关键字:变量值 变量 值传 作用域 另一个 简单 angularjs 最佳      更新时间:2023-09-26

我想从当前作用域外访问变量。我想使用从外部访问变量的最佳实践。

我的需求:让我告诉我的情况,我已经在一个范围内从收集数据从选择选项在这个成功范围和在下一次成功,我将获取json数据使用这个url,我将如何做

DEMO: http://plnkr.co/edit/wIk0dwiDwgWlOLrAvjX5?p=preview

 $http.get("data.json")  // this will give some data from which i will made select options
.success(function(response) {  
      // generate dynamic url        
      var dyanamic_url="http://192.168.206.133:8080/admin/metering/<select_options>;
      //alert(url);
    });
  //I wanted to use this dyanamic_url outside scope.
 //Outside the scope of success.
 $http.get(dyanamic_url)
.success(function(response) {
      // Fetch JOSN DATA
})

以下操作是否有效?

.success(function (response) { 
       var url = response.someproperty; 
       http$.get(url).success(function(respone ))  
 });

不需要访问该范围内的url。相反,只需在success回调中发送第二个请求。

.success(function (response) {
    var url = response.someproperty;
    sendAnotherRequest(url);
});