从angularjs范围对象获取值

时间:2017-01-20 06:57:32

标签: php angularjs

我有以下angularjs代码用于向php发布值:

$scope.aToUn = function(aName) {
    //$scope.question.question_16 =UnionsId;
    var outputType=[{type:'aName',action:'none',PassId:aName}];

    $http.post('addressManagement.php',outputType).success(function(dataJsonUnions) {
        //alert(dataJsonUnions);
        $scope.question =dataJsonUnions;

        //question or dataJsonUnions is an object that passed from json, 
        //something like question={{id="2",questionvalue="Name"}}
        //calling another function 
        $scope.anotherFunction(questionvalue); // here i want to pass only value that from json eg. 'Name' only)
    });
};

我正在获取json对象值,我想用只有单个值做一些事情。 (对象只包含一个值,不想if / else或foreach)

3 个答案:

答案 0 :(得分:1)

你试过吗

$scope.anotherFunction($scope.question[0].questionvalue);

答案 1 :(得分:0)

正如我理解你的问题,你首先需要通过使用angular.fromJson()然后传递对象的属性来将json响应转换为对象:

$scope.question = angular.fromJson(dataJsonUnions);

然后使用您要传递的参数调用函数:

 $scope.anotherFunction($scope.question.questionvalue);

希望这会对你有所帮助。

答案 2 :(得分:0)

如果响应是JSON对象,并且您想要访问字段' questionValue'的值。然后你可以尝试以下。

var json = JSON.parse(dataJsonUnions);
json.questionValue; // THis will fetch you the value
//OR
json['questionValue'];
// Then you can pass this value to any function that you need