一个模型无法访问,一个是

时间:2014-05-03 12:45:50

标签: angularjs twitter-bootstrap-3

我有两个小项目,两者都使用基本相同的设置,一个返回模型值,一个返回模型值为undefined。在这一点上,我迷失了为什么这两个例子表现不同。我查看了失败的示例,模型正确填充

成功的行为 部分

<div class="row">
<div class="col-md-4 .col-md-offset-4 content-pane no-margin">
    <h4>Input Your Inquiry</h4>
    <form ng-submit="submit(invoice)">
        <div class="control-group">
                  <div class="controls">
                    <div class="input-prepend">
                      <span class="add-on">Invoice Number</span>
                      <input id="who" type="text" class="input-xlarge"     ng-model="invoice.number">
                    </div>
                  </div>
                </div>
                <div class="control-group">
                  <div class="controls">
                    <div class="input-prepend">
                      <span class="add-on">PO Number</span>
                      <input id="who" type="text" class="input-xlarge" ng-model="invoice.po">
                    </div>
                  </div>
                </div>
        <input type="submit">
    </form>
</div>
<div ng-if="status" class="col-md-4 .col-md-offset-4 content-pane no-margin">
    <h4>Invoice Status</h4>
    <div>{{status}}</div>
</div>
</div>

App.js

app.config(function($routeProvider){

    $routeProvider
        .when('/',
            {
                templateUrl: 'app/partials/inquiry-input-form.html',
                controller: 'MainCtrl'
            })
        .otherwise(
            {
            templateUrl: 'app/partials/404.html'
            })
});

app.controller('MainCtrl', function($scope,formsResults) {
    $scope.submit = function() {
        var invoiceNum = $scope.invoice.number;
        var po = $scope.invoice.po;
        formsResults.getInvoiceStatus(po,invoiceNum).success(function(data) {
            var status = data.records;

            if (angular.isUndefined(status)) {
                $scope.status = 'Pending Reciept or Entry';
            } else {
                $scope.status = status[0].field_124208[0];
            }
        });
    };
});

app.factory('formsResults', function($http) {............

在控制器中将模型返回为未定义 部分

<tabset>
    <tab active=workspace.active>
        <tab-heading>
            Search All Fields
        </tab-heading>
        <div>
        <br /><br />
            <form class="form-horizontal" ng-submit="submitAll(searches)">

                <div class="form-group">
                  <div class="col-md-4">
                  <input id="textinput" name="textinput" type="text" class="form-control input-md" ng-model="searches.term">
                  <span class="help-block">Enter the term you wish to search on.</span>  
                  {{searches.term}}
                  </div>
                </div>

                <!-- Button -->
                <div class="form-group">
                  <div class="col-md-4">

                    <button id="singlebutton" name="singlebutton" class="btn btn-primary" type="submit">Search</button>
                  </div>
                </div>

            </form>     
        </div>
    </tab>
    <tab>
        <tab-heading>
            Search By Field
        </tab-heading>
        <div>
        <br /><br />
            <form class="form-horizontal" ng-submit="submitSpecific(search)">
                <div class="form-group">
                  <div class="col-md-4">
                    <select id="selectbasic" name="selectbasic" class="form-control">

                      <option value="4">Country</option>
                    </select>
                    <span class="help-block">Enter the field you wish to search on.</span>  
                  </div>
                </div>

                <!-- Text input-->
                <div class="form-group">
                  <div class="col-md-4">
                  <input id="textinput" name="textinput" type="text" class="form-control input-md">
                  <span class="help-block">Enter the term you wish to search on.</span>  
                  </div>
                </div>

                <div class="form-group">
                  <div class="col-md-4">

                    <button id="singlebutton" name="singlebutton" class="btn btn-primary" type="submit">Search</button>
                  </div>
                </div>
            </form>
        </div>
    </tab>
</tabset>

App.js

var app = angular.module ('app',['ngRoute','ngSanitize','ui.bootstrap','dialogs.controllers','dialogs']);

app.config(function($routeProvider){
    //http://docs.angularjs.org/tutorial/step_07
    $routeProvider
        .when('/',
            {
                templateUrl: 'app/partials/home_tpl.html',
                controller: 'HomeCtrl'
            })
        .when('/search',
            {
                templateUrl: 'app/partials/search-form.html',
                controller: 'SearchCtrl'
            })
        .when('/add-sop',
            {
                templateUrl: 'app/partials/support-process.html',
                controller: 'EditSOPCtrl'
            })
        .otherwise(
            {
            redirectTo: '/'
            })
});

app.controller('HomeCtrl', function($scope) {

});

app.controller('SearchCtrl', function($scope,formsSopSearchCriteria) {

    $scope.submitAll = function() {
        var searchTerm = $scope.searches.term;
        console.log(searchTerm);
        var searchPackage={
                "offset":0,
                "limit":0,
                "fields":["record_id","dt_created","created_by","dt_updated","updated_by",149654,149655,149692],
                "filters":{"and":[{"field_id":"149655","operator":"contains","value":searchTerm}]},
                "sort":{}
        };
        searchPackage=JSON.stringify(searchPackage);
        $scope.searchPackage = searchPackage;

        formsSopSearchCriteria.getMatches(searchPackage).success(function(data) {
            $scope.sopRecords = data.records;
        });
    };

    $scope.submitSpecific = function() {

        var searchPackage = ""
        searchPackage=JSON.stringify(searchPackage);
        $scope.searchPackage = searchPackage;

        formsSopSearchCriteria.getMatches(searchPackage).success(function(data) {
            $scope.sopRecords = data.records;
        });
    };

});


app.factory('formsSopSearchCriteria',function($http) {........

有什么想法吗?我的理解是它是一个范围问题。但是在失败的示例中,我提供的部分映射到routeProvider中的SearchCtrl。因此,模型应该在控制器中可用,就像正在工作的示例一样。我怀疑我必须遗漏一些东西,但我不清楚我错过了什么。

1 个答案:

答案 0 :(得分:0)

您需要在初始化时将$scope.searches创建为空对象:

app.controller('SearchCtrl', function($scope,formsSopSearchCriteria) {

  $scope.searches = {};

或者为了清楚起见:

$scope.searches =
{
  term: ''
};

如果您没有,则在输入第一个输入之前不会创建它(我相信$parse指令使用的ng-model服务会在此创建搜索对象情况)。

问题是可能无法在您想要的范围内创建对象。

它在第一种情况下工作的原因是因为没有子范围,所以invoice对象是在控制器范围内创建的。

在第二种情况下,有一个或多个子范围(可能来自选项卡,我还没有深入挖掘它),因此searches对象是在其中一个中创建的。< / p>

由于原型继承在Javascript中的工作原理,如果你预定义控制器中的对象&#39;范围,将正确创建属性。