rizal rizal - 5 months ago 9
Javascript Question

AngularJS pass filtered value to scope value

Good morning, i'm new to angularjs and want to practice it more, and here i have a question regarding a simple case while trying to learn to build my own angularjs webpage.

I have these two sets of data

$scope.data1 = [{ id: 1, name: "abc" },
{ id: 2, name: "efg" }]
$scope.data2 = [{ id: 1, info: "this is abc" },
{ id: 2, info: "absolutely not abc"}]
$scope.user = {
id: "",
name: "",
info: ""
}


and i have this input

<input ng-blur="passTheValue()" ng-model="user.idx" ng-type="text" placeholder="write name here"></input>


where i can write name on the text box.

my question is, how to pass all the value from data1 and data2 to $scope.user based on what input i have entered? For example, i write "abc" on the textbox, then my $scope.user will contain

id: 1, name: "abc", info: "this is abc"


i've tried to use $filter but i'm stuck at passing the value to the scope.

i'm sorry for my English, it's not my main language.

Answer

This is not the classical usecase for a filter: Do the processing of your data in the passTheValue() function.

this.passTheValue = function(){
    $scope.data1.forEach(function(value, index, array){
        if(value.name == $scope.idx){
            $scope.user = {id: value.id, name: value.name, info: $scope.data2[index] }
        }
    })
}
Comments