簡體   English   中英

angularJS-ng模型未更新

[英]angularJS - ng-model is not updating

在研究了有關AngularJS一些教程之后,我現在自己編寫第一個示例應用程序。 我首先顯示了一個非常簡單的員工列表,效果很好。 現在,我想添加一個簡單的文本過濾器,就像在教程中學到的那樣。

我添加了帶有ng-model="filterText"的輸入,並且| filter: filterText | filter: filterText到我的html中的列表以及$scope.filterText = null; 到我的angularJS控制器。

現在,當我在輸入中輸入任何內容時,什么也沒有發生。 當我直接向AngularJS控制器設置filterText值時,過濾器正在運行,因此更新textFilter的值時必然存在問題。

我該怎么做才能使其正常工作? 我已經在尋找解決方案,但是沒有任何幫助。

我的html:

<div class="container main-frame" ng-app="Employees" ng-controller="mainController" ng-init="init()">

<div id="searchbox">
    <label>Filter: </label>
    <input type="text" ng-model="filterText" />
</div>

<div id="emplist">

    <h2>Employees</h2>

    <p>
        <ul id="emps">
            <li ng-repeat="mitarbeiter in results | filter: filterText">
                # {{mitarbeiter.id}} - <strong>{{mitarbeiter.name}}</strong>
            </li>
        </ul>
    </p>

</div>

我的angularJS:

var app = angular.module('Employees', []);

app.controller("mainController", function ($scope) {

    $scope.results = [];
    $scope.filterText = null;

    $scope.init = function(){

        jsonObject = eval(jsonfunction("parameters"));

        angular.forEach(jsonObject, function (mitarbeiter, key) {

            $scope.results.push(mitarbeiter);

        });

    }

})

編輯:

根據NidhishKrishnan的回答:

在firebug中,我的jsonObject如下所示:

[{"id":1,"name":"John"},{"id":2,"name":"Jane"},{"id":3,"name":"Peter"}]

我使用此jsonObject更改了工作解決方案,但它仍然可以正常工作,因此這不應該是問題...

進一步的信息:我正在使用Web api 2控制器以VS 2013調試模式工作,該控制器獲取sql數據庫的數據。 我的jsonfunction就是向控制器發出的ajax請求。

EDIT2:

當我不使用eval() ,沒有任何變化。 我已成功獲取列表,但無法使用過濾器...這是我的Ajax請求:

function jsonfunction(par) {

    $.ajax({

        url: url + par,

        async: false,

        success: function (data) {

            json = data;

        },

        headers: getSecurityHeaders()

    });

    return json;

}

答案在我的WebApiConfig.cs格式化為JSON。 不會有任何錯誤...

jsonObject javascript變量應獲取一些數據,如下所示

jsonObject=[{id:1,name:'Jessey'},
            {id:2,name:'John'},
            {id:3,name:'Mathew'},
            {id:4,name:'Sunny'}];  

但是在您的代碼中jsonfunction ,而是使用eval評估的目的是什么,我們需要返回一個JSON那么所有過濾器都可以按預期工作

jsonObject = eval(jsonfunction("parameters"));  

也不AngularJS JQueryAngularJS混合使用,因為它不是一個好習慣,請閱讀- 將jQuery UI插件與Angular一起使用

對於AJAX AngularJS本身提供了$ http

一個有效的例子

編輯1

$scope.init = function()
{
        $http({
            method: 'GET',
            url: '/someUrl'
             }).
        success(function (data, status, headers, config) {
            angular.forEach(data, function (mitarbeiter, key) {
                $scope.results.push(mitarbeiter);
            });
        }).
        error(function (data, status, headers, config) {
        });    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM