为什么我的ng-model变量在控制器中未定义?

Dev*_*v01 12 javascript angularjs

我在angular js项目中遇到$ scope问题.例如,当我在输入字段上使用ng-model ="modelExample"时,我无法使用$ scope.modelExample在我的js中访问它.有没有其他人有类似的问题?

奇怪的是,一个函数被调用,但ng-model没有绑定.请参阅下面的代码,我提交表单时调用函数refreshResults(),但$ scope.search返回undefined.

angular.module('starter', 
    ['ionic', 
    'starter.controllers', 
    'starter.filters', 
    'akoenig.deckgrid', 
    "angucomplete",
    // 'ui.bootstrap', 
    'starter.services'])

.config(function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('app', {
            url: "/app",
            abstract: true,
            templateUrl: "templates/menu.html",
            controller: 'AppCtrl'
        })

        .state('app.browse', {
            url: "/browse",
            views: {
                'menuContent' :{
                    templateUrl: "templates/browse.html",
                    controller: 'BrowseCtrl'
                }
            }
        })

        .state('app.search', {
            url: "/search",
            views: {
                'menuContent' :{
                    templateUrl: "templates/search.html",
                    controller: 'SearchCtrl'
                }
            }
        })
  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/app/browse');
});


angular.module('starter.controllers', [])
.controller('SearchCtrl', function($scope) {
    $scope.refreshResults = function() {
        console.log($scope.search);
    };
})


<ion-view>
    <ion-content class="has-header">
        <form ng-submit="refreshResults()" class="bar bar-header item-input-inset">
            <label class="item-input-wrapper">
                <i class="icon ion-ios7-search placeholder-icon"></i>
                <input type="search" placeholder="Search..." ng-model="search">
            </label>
        </form>
    </ion-content>
</ion-view> 
Run Code Online (Sandbox Code Playgroud)

Dev*_*v01 37

@DavidTryon在评论中解决了我的问题.我使用范围错了.我需要使用一个对象而不是一个字符串.换句话说,我需要做一些ng-model="search.term"不喜欢的事情ng-model="search",并在像这样的js中使用它:$scope.search.term.它与继承有关,但长话短说,如果你的ng模型中没有一个点(.),那你做错了.

这里有更多信息:http: //jimhoskins.com/2012/12/14/nested-scopes-in-angularjs.html

谢谢大卫!

汤姆