2017-03-18 7 views
0

私はnoobです。私はちょうど私のWeb開発証明書を持っています。私はAngularjsにも新しいです。 1)サイト訪問者からの入力を受け取ります。 2)入力を使用してAPI呼び出しを行います。 3)divを表示させます。 4)divをデータで埋め込みます。Angularjs:複数のアクションをトリガーするボタン

これらはすべて動作します。 この作業を行うには:5)データを表示するdivにスクロールします。コントローラにコードがありますが、何か間違ったことがあり、サイトが自動的にデータを含むdivにスクロールしません。 私は、ボタンのクリックがいくつかのイベントやdomへの変更をトリガできるようにすることが有用であると思います。

私はPlunkerのようなサンプルサイトにこれを置く方法を理解していません。私は下に私のコントローラコードを貼り付けることができます。 レポは:https://github.com/MikeGalli/wats4030-capstone_v2 サイトは:http://thisisourgov.com/ このボタンで達成するのがもう1つ良いです:6)訪問者の入力をクリアします。 ありがとうございました。

マイコード:

angular.module('wats4030CapstoneV2App') 
    .controller('MainCtrl', function($scope, current, repsearchfed, repsearch) { 
    $scope.current = current.query(); 
    $scope.refreshCurrent = function(location) { 
     $scope.current = current.query({ 
     location: location 
     }); 

     //// Start Make the div visiable ///////////////// 
     $scope.IsVisible = $scope.IsVisible ? false : true; 
     //// End Make the div visiable ///////////////// 

     //// Start repsearch ///////////////// 
     $scope.current.$promise.then(function(data) { 
     $scope.repsearch = repsearch.query({ 
      lat: data.results[0].geometry.location.lat, //This is the Google search 
      lng: data.results[0].geometry.location.lng 
     }); 
     }); 
     //// End repsearch ///////////////// 

     //// Start repsearchfed ///////////////// 
     $scope.current.$promise.then(function(data) { 
     $scope.repsearchfed = repsearchfed.query({ 
      lat: data.results[0].geometry.location.lat, //This is the Google search 
      lng: data.results[0].geometry.location.lng 
     }).then(function(repdata) { 
      $scope.repdata = repdata.data; 
     }); 
     }); 
     //// End repsearchfed ///////////////// 

     //// Start Scroll to div ///////////////// 
     $scope.window = (function scrollWin() { 
     window.scrollTo(0, 500); 
     $scope.refreshCurrent.$setUntouched(); 
     $scope.refreshCurrent.$setPristine(); 
     }); 
     //// End Scroll to div ///////////////// 

    }; 
    }); 

答えて

0

あなたは、$qサービスhttps://docs.angularjs.org/api/ng/service/ $ qを使用するようにコードをリファクタリングすることができます$q.allがパラメータとして約束の配列を期待し、配列の各約束が解決されたときに解決された約束を返します。このサービスを利用すると、クエリに競争上の問題に陥ることはありません。

angular.module('wats4030CapstoneV2App') 
    .controller('MainCtrl', function($scope, $q, current, repsearchfed, repsearch) { 
    $scope.current = current.query(); 
    $scope.refreshCurrent = function(location) { 
     $scope.current = current.query({ 
     location: location 
     }); 

     //// Start Make the div visiable ///////////////// 
     $scope.IsVisible = $scope.IsVisible ? false : true; 
     //// End Make the div visiable ///////////////// 

     // Start search 
     $scope.current.$promise.then(function(data) { 
     // Wait both queries, repsearch and repsearchfed 
     $q.all([ 
      repsearch.query({ 
      lat: data.results[0].geometry.location.lat, //This is the Google search 
      lng: data.results[0].geometry.location.lng 
      }), 
      repsearchfed.query({ 
      lat: data.results[0].geometry.location.lat, //This is the Google search 
      lng: data.results[0].geometry.location.lng 
      }) 
     ]).then(function (response) { 
      // Search finished 
      var repdata = response[0], 
      repfeddata = response[1]; 
      $scope.repdata = repdata.data; 
      $scope.repfeddata = repfeddata.data; 
      // Clear input text 
      $scope.location = ''; 
      // Scroll to section 
      window.scrollTo(0, 500); 
     }) 
     }); 
     //// Start repsearch ///////////////// 

     // $scope.current.$promise.then(function(data) { 
     // $scope.repsearch = repsearch.query({ 
     //  lat: data.results[0].geometry.location.lat, //This is the Google search 
     //  lng: data.results[0].geometry.location.lng 
     // }); 
     // }); 
     // //// End repsearch ///////////////// 

     // //// Start repsearchfed ///////////////// 
     // $scope.current.$promise.then(function(data) { 
     // $scope.repsearchfed = repsearchfed.query({ 
     //  lat: data.results[0].geometry.location.lat, //This is the Google search 
     //  lng: data.results[0].geometry.location.lng 
     // }).then(function(repdata) { 
     //  $scope.repdata = repdata.data; 
     // }); 
     // }); 
     //// End repsearchfed ///////////////// 

     //// Start Scroll to div ///////////////// 
     // $scope.window = (function scrollWin() { 
     // window.scrollTo(0, 500); 
     // $scope.refreshCurrent.$setUntouched(); 
     // $scope.refreshCurrent.$setPristine(); 
     // }); 
     //// End Scroll to div ///////////////// 

    }; 
    }); 
0

スクロールを開始する関数の最初の行を書き換えることができます。基本的に同じ$ promiseを待ってから、scrollWin関数を適用します。

//// Start Scroll to div ///////////////// 
$scope.current.$promise.then(function scrollWin() { 
    window.scrollTo(0, 1000); 
    $scope.refreshCurrent.$setUntouched(); 
    $scope.refreshCurrent.$setPristine(); 
}); 
//// End Scroll to div ///////////////// 
関連する問題