AngularJS正确传递变量的方法

时间:2016-08-28 21:40:57

标签: javascript angularjs

我想让它们全局,这样我就可以在整个脚本中使用[color][shape]。我将需要每个人独立更新,但随着我继续添加到网站,我将需要一起使用。 Live preview

  • 示例不起作用:$scope.shapeSelected = response.data[color][shape];
  • 示例有效:$scope.shapeSelected = response.data.blue[shape];
var app = angular.module("computer", ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
    when('/main', {
        controller: 'MainCtrl'
    }).
    otherwise({
        redirectTo: '/main'
    })
}])

.controller('MainCtrl', ['$scope', '$http', function($scope, $http) {

    $scope.colorType = function(color) {
        $http.get('stuff.json').then(function(response) {
            $scope.colorSelected = response.data.type[color];

        });
    }

    $scope.shapeType = function(shape) {
        $http.get('shapes.json').then(function(response) {
            $scope.shapeSelected = response.data[color][shape]; // <--- [color] is not getting pulled in on this function.

            var resultsColorShape = $scope.shapeSelected; // <--- I would like to be able to store this incase i need it later. 
            console.log('resultsColorShape');
        });
    }

}]);

2 个答案:

答案 0 :(得分:1)

您不必将参数传递给您的函数。如果您定义了task.Result.Save(@"C:\images\file" + count + ".jpg"); ,则可以在JavaScript代码中使用ng-model="Color"

更改html:

$scope.Color

和js to:

ng-change="colorType()"

ng-change="shapeType()"

答案 1 :(得分:0)

如果您的问题是关于传递变量或在各个函数之间正确共享数据,那么这应该对您有帮助。

在您的方案中。由于分配了ng-change个函数。

  1. 如果您没有ng-change尝试保存ng-model中传递的参数的新值以便访问$scope功能,则触发ng-model功能跨越该控制器中的所有其他功能。
  2. 如果您为元素声明了ng-model="xyz",那么只需使用ng-model的属性作为参考变量。例如:$scope.xyz然后 @Override protected Void doInBackground(Void... params) { try { Connection.Response baglanilan = Jsoup.connect("http://www.eshot.gov.tr/tr/UlasimSaatleri/288") .method(Connection.Method.GET) .execute(); Document document = Jsoup.connect("http://www.eshot.gov.tr/tr/UlasimSaatleri/288").data("hatId","581").cookies(baglanilan.cookies()).post(); Elements bakiye = document.select("#frmDuraklar > ul > li"); veri1ad = bakiye.toString(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); progressDialog.dismiss(); tvveri.setText(veri1ad); } 会为您提供所需的元素值。
  3. 这是您可以相应地访问元素值的方法

相关问题