1

So I have a search box that I enter text into then submit, this should send the data to my Angular controller and allow me to access it however I'm just getting undefined when doing so.

The issue is caused when trying to access the field by using $scope.searchtext

My code is as follows:

angular.module('starter.controllers', [])

.controller('SearchCtrl', function ($scope, $http, $ionicLoading) {

$scope.results = [];

$scope.search = function () {
    $ionicLoading.show({ template: 'Loading...' });

    $http.get('https://www.googleapis.com/youtube/v3/search?part=snippet&q=' + $scope.searchtext + '&key=').
        then(function (data) {

            var searchResults = data

            angular.forEach(searchResults.data.items, function (val, i) {

                $http.get('https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=' + val.id.videoId + '&key=').
                then(function (data) {

                    var durationResults = data
                    var duration = durationResults.data.items[0].contentDetails.duration.replace("PT", "").replace("H", ":").replace("M", ":").replace("S", "")

                    $scope.results.push({ thumbnail: val.snippet.thumbnails.medium.url, title: val.snippet.title, channel: durationResults.data.items[0].snippet.channelTitle, description: val.snippet.description, duration: duration })
                });

            });
        });

    $ionicLoading.hide();
};

})


<ion-view title="Search for Music">

    <ion-content scroll="true" class="has-header padding">

        <form ng-submit="search()" class="list list-inset">
            <label class="item item-input">
                <i class="icon ion-search placeholder-icon"></i>
                <input type="search" name="searchtext" ng-model="searchtext" placeholder="Search">
            </label>
        </form>

        <ion-list>
            <ion-item ui-sref="home" ng-repeat="result in results" class="item item-thumbnail-left">
                <img src="{{result.thumbnail}}">
                <h2>{{result.title}}</h2>
                <strong>{{result.channel}}</strong>
                <p>{{result.description}}</p>
                <p>Duration: {{result.duration}}</p>
            </ion-item>
        </ion-list>

    </ion-content>
</ion-view>

2 Answers 2

1

ion-content does create child scope dealing with that DOM, In that case you can use AngularJS Prototypal inheritance.

Code

.directive('ionContent', [
  '$parse',
  '$timeout',
  '$ionicScrollDelegate',
  '$controller',
  '$ionicBind',
function($parse, $timeout, $ionicScrollDelegate, $controller, $ionicBind) {
  return {
    restrict: 'E',
    replace: true,
    transclude: true,
    require: '^?ionNavView',
    scope: true, //<-- this creates a prototypically inherited scope
    template:
    '<div class="scroll-content">' +
      '<div class="scroll"></div>' +
    '</div>',

Do follow dot rule while declaring ng-model like define searchtext as an object property that will tend to follow javascript prototypal.

Markup

<input type="search" name="searchtext" ng-model="model.searchtext" placeholder="Search">

Controller

$scope.model = {};

Similar SO answer here

Sign up to request clarification or add additional context in comments.

2 Comments

Ah I see, apologies, I'm new to Angular. That's fixed my issue!
@TomWilson np..Glad to help you..Thanks
0

Use can also use $parent to access the value of controller.

<input type="search" name="searchtext" ng-model="$parent.searchtext" placeholder="Search"> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.