javascript - 范围值不反射(reflect)在 View 中

标签 javascript angularjs asp.net-mvc-3

我真的需要你的帮助...我在更新 ASP.Net MVC/AngularJS 中的范围时遇到问题。

$scope.svFullName 的问题...当我更改其值时,即使它在 JS 代码中发生更改,它也不会反射(reflect)在 View 中,因为我已经从 js 中对其进行了测试。这是我的代码:

查看:

<td ng-repeat="x in supervisors | limitTo:1" style="width: 60%;">
<div ng-show="!svElements">{{svFullName}}</div>
<div ng-show="svElements">
    <select ng-model='svFullName' ng-selected='svFullName'>
        <option ng-repeat="z in supervisorsList">{{z.supervisorfName + " " + z.supervisorlName}}</option>
    </select>
    <a href="javascript:void(0)">Save</a>                                                                                                
</div>


编辑 节省

JS:

var myApp = angular.module('myApp', []);
myApp.controller('mainController', function ($scope, $http) {
$http.get('/Home/GetSupervisor')
    .then(function (response) {
        $scope.supervisors = response.data;
        $scope.svFullName = response.data[0].supervisorfName + " " + response.data[0].supervisorlName;
        $scope.defaultsvFullName = $scope.svFullName;
    })
    .catch(function (e) {
        console.log("error", e);
        throw e;
    })
    .finally(function () {
        console.log("This finally block");
    });

$http.get('/Home/GetSupervisorsList')
    .then(function (response) {
        $scope.supervisorsList = response.data;
    })
    .catch(function (e) {
        console.log("error", e);
        throw e;
    })
    .finally(function () {
        console.log("This finally block");
    });

$scope.svElements = false;
$scope.toggleSV = function () {
    if ($scope.svElements) {
        $scope.svFullName = $scope.defaultsvFullName;
    }
    $scope.svElements = !$scope.svElements;
}
});

$scope.svFullNamediv 绑定(bind)在一起,因此当我单击编辑并更改名称时,$scope.svFullName 将被更改...我想要做的是,当我单击“取消”时,将 $scope.svFullName 更改回默认值。

该值未反射(reflect)在 View 中

最佳答案

UPDATED to online version with $http get... you can return object or string or id, see the example

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

        app.controller("ctrl", function ($scope, $http) {
            $scope.showList = true;
            $scope.defaultValue = {};

            var root = "http://jsonplaceholder.typicode.com";

            $http.get(root + "/users").success(function (response) {
                $scope.supervisorsList = response;
                $scope.defaultValue = $scope.supervisorsList[0];
                $scope.svFullName = $scope.supervisorsList[0];
            });

            //default

            //reset
            $scope.reset = function () {
                $scope.showList = false;
                $scope.svFullName = $scope.defaultValue;
            }

        });
<!DOCTYPE html>
<html ng-app="app" ng-controller="ctrl">
<head>
    <title></title>
</head>
<body>

    <div>{{svFullName.name}}</div>
    <div>
        <select ng-model="svFullName" ng-show="showList" ng-options="z as z.name for z in supervisorsList"></select>
        <button ng-show="showList" ng-click="showList = false">save</button>
        <button ng-show="showList" ng-click="reset()">cancel</button>
        <button ng-hide="showList" ng-click="showList = true">change</button>
    </div>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  
</body>
</html>

关于javascript - 范围值不反射(reflect)在 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38025270/

相关文章:

javascript - js - appendChild 方法破坏整个页面

c# - 在下拉列表中设置选定值

asp.net - .net MVC 3 请求事件

javascript - 无法执行事件ajaxStart()

javascript - 如何使用 ServerOperation 获取 Kendo Grid 项目的页面

javascript - ngGrid 多选取消当前选择

javascript - 禁用按钮触发器直到动画完成(angularjs)

javascript - 选择框中的 Angular , boolean 值

asp.net-mvc - Actionresult 与 JSONresult