在ui-router中的状态之间共享ng-model数据

时间:2015-11-23 13:21:53

标签: javascript angularjs angular-ui-router

我似乎无法通过此表单更新我的模型。它使用ng-route工作,但我决定更改为ui-router,因为我想嵌套模板。

我从这里尝试了一些事情How do I share $scope data between states in angularjs ui-router?,但没有运气。

home.html的

<form class="input-lg m-t m-l-n-xs hidden-lg hidden-md hidden-sm" role="search">
  <div class="form-group">
    <div class="input-group">
      <span class="input-group-btn">
        <button type="submit" class="btn btn-sm bg-white btn-icon rounded"><i class="fa fa-search"></i></button>
      </span>
      <input type="text" ng-model="searchQuery" ng-change="changeView('search')" class="form-control input-sm no-border rounded" placeholder="Search songs, albums...">
    </div>
  </div>
</form>

控制器

  angular.module('musica', ['ui.bootstrap', 'ngFacebook', 'spotify', 'cb.x2js', 'ngRoute', 'ui.router'])
   .config(function ( $facebookProvider, SpotifyProvider, $routeProvider, $httpProvider, $stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise("/");
    $stateProvider
        .state('login', {
        url: "/",
            templateUrl: "partials/login.html",
        }).state('home', {
            templateUrl: "partials/home.html",
        }).state('home.select', {
        templateUrl: "partials/home.select.html"
        }).state('home.search', {
            templateUrl: "partials/home.search.html",
        }).state('home.queue', {
        templateUrl: "partials/home.queue.html"
        }).state('join', {
        templateUrl: "partials/join.html"
        }).state('creating', {
        templateUrl: "partials/creating_venue.html"
        }).state('home.server', {
        templateUrl: "partials/home.server.html"
        })

})

.controller( 'DemoCtrl', ['$scope','$rootScope', '$location', '$facebook', 'Spotify', 'x2js', '$http', '$state', 'globalStore', function ($scope, $rootScope, $location, $facebook, Spotify, x2js, $http, $state, globalStore) {

$scope.searchQuery='hello';

$scope.changeView = function(view){
        if(!$state.is('home.search'))
        {
            $state.go('home.search');
        } 
        $scope.searchTrack();
    };

$scope.searchTrack = function () {
    console.log('searching ' + $scope.searchQuery.query);
    Spotify.search(this.searchQuery.query, 'track').then(function (data) {
        $scope.tracks = data.tracks.items;
    });
};

如果我然后输入搜索框,我得到的是hello而不是更新的值。我认为,因为我没有提到状态提供者中的控制器,只有一个控制器会被实例化。我真的很想知道发生了什么。非常感谢你。

0 个答案:

没有答案