下拉列表更改时,ng-Options不会刷新有线标签

时间:2017-01-25 05:53:39

标签: angularjs ng-options

遵循Scott Allen的第一个代码(简单)sample,在下拉条目更改后,有线ng-model不会刷新此

  

{{engineer.currentActivity}}

浏览器:FF 50.1.0

Angular:1.5.9

jQuery:1.7

HTML:

<div ng-controller="EngineeringController">
        {{engineer.name}} is currently : {{engineer.currentActivity}}
        <div>
            choose an activity:
            <select id="agDDLClient" ng-model="EngineeringController.currentActivity" ng-options ="act for act in activities">
            </select>
        </div>
        <input type="button" ng-click="what()" value="check" />
    </div>

JS:

var aIS = angular.module("app", []);
aIS.controller("EngineeringController", function($scope, $http)
{
    $scope.engineer = {
    name: "Dani",
    currentActivity: "Fixing bugs"
};

$scope.activities =
[
    "Writing code",
    "Testing code",
    "Fixing bugs",
    "Dancing"
];

$scope.what = function(){ alert($scope.engineer.currentActivity);}
});

2 个答案:

答案 0 :(得分:0)

更改为ng-model="engineer.currentActivity"而不是ng-model="EngineeringController.currentActivity"

并且您的控制器代码也应遵循此

var aIS = angular.module("app", []);
aIS.controller("EngineeringController", function($scope, $http)
{
    $scope.engineer = {
    name: "Dani",
    currentActivity: "Fixing bugs"


$scope.activities =
[
    "Writing code",
    "Testing code",
    "Fixing bugs",
    "Dancing"
];

$scope.what = function(){ alert($scope.engineer.currentActivity);}
});

答案 1 :(得分:0)

当您尝试提醒ng-modelengineer.currentActivity函数时,

EngineeringController.currentActivity值应为$scope.engineer.currentActivity而不是what()

工作演示:

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

myApp.controller('EngineeringController',function($scope) {
    $scope.engineer = {
        name: "Dani",
        currentActivity: "Fixing bugs"
    };

    $scope.activities = [
        "Writing code",
        "Testing code",
        "Fixing bugs",
        "Dancing"
    ];

    $scope.what = function() { 
        alert($scope.engineer.currentActivity);
    };
    
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="EngineeringController">
        {{engineer.name}} is currently : {{engineer.currentActivity}}
        <div>
            choose an activity:
            <select id="agDDLClient" ng-model="engineer.currentActivity" ng-options ="act for act in activities">
            </select>
        </div>
        <input type="button" ng-click="what()" value="check" />
    </div>