简体   繁体   English

Angular指令ng-model适用于数组,但不适用于字符串

[英]Angular directive ng-model working for arrays but not string

The ng-model directive seems to be lacking a reference to the actual object within the JavaScript, but only for string values. ng-model指令似乎缺少对JavaScript中实际对象的引用,但仅针对字符串值。 Using the list of dictionary objects and looping over the elements with ng-repeat as shown below though, it works. 使用字典对象列表并使用ng-repeat遍历元素,如下所示,它可以工作。

I can only think that it may be due to returning the array acts like returning a reference to the object, whereas returning the string variable is simply returning the literal string, neutralizing the Angular's ability to do it's two-way data binding and leaving me with a variable that still holds a value of undefined . 我只能认为这可能是由于返回数组的行为就像返回对对象的引用一样,而返回字符串变量只是返回文字字符串,抵消了Angular进行双向数据绑定的能力,这使我无所适从一个仍然保持undefined值的变量。

Why is my service module below unable to pull the updated value from the view for the variable gitRelease ? 为什么下面的服务模块无法从视图中获取变量gitRelease的更新值?

In a service module I have this functionality: 在服务模块中,我具有以下功能:

(function () { //start iife

    'use strict';

    angular.module('gms.autoDeploy')
        .factory('AutoDeployService', ["$http", "$q", "$log", "$cookies", "APP_CONFIGS", "SweetAlert", "$timeout", "GridSettingsService", "APP_USER", AutoDeployService]);

    function AutoDeployService($http, $q, $log, $cookies, APP_CONFIGS, $timeout, SweetAlert, GridSettingsService, APP_USER) {

        var tibcoCopyJobs = [];
        var gitRelease = "";

        function addNewTibcoCopyJob() {
            tibcoCopyJobs.push({
                sourceServer: "",
                sourcePath: "",
                destinationServer: "",
                destinationPath: ""
            });
        }

        function getTibcoCopyJobs() { return tibcoCopyJobs; }
        function getGitRelease(){ return gitRelease; }

        function extractFormData() {
            console.log(gitRelease);
            for (var i = 0; i < tibcoCopyJobs.length; i++) {
                console.log(tibcoCopyJobs[i]);
            }
        }

        return {
            addNewTibcoCopyJob:             addNewTibcoCopyJob,
            getTibcoCopyJobs:               getTibcoCopyJobs,
            getGitRelease:                  getGitRelease,
            extractFormData:                extractFormData
        };
    } //end AutoDeployService
}()); //end iife

Using it with this controller: 与此控制器一起使用:

angular.module("gms.autoDeploy").controller('AutoDeployController', ['$scope', '$compile', 'AutoDeployService',
function ($scope, $compile, AutoDeployService) {

        var model = this;

        init();

        function init() {
            model.tibcoCopyJobs = AutoDeployService.getTibcoCopyJobs();
            model.gitRelease = AutoDeployService.getGitRelease();
        }

        function btn_addNewTibcoCopy() { AutoDeployService.addNewTibcoCopyJob(); }

        function btn_extractFormData() { AutoDeployService.extractFormData(); }

        model.btn_addNewTibcoCopy = btn_addNewTibcoCopy;
        model.btn_extractFormData = btn_extractFormData;
    }
]);

To give functionality to this view: 为该视图提供功能:

<div ng-controller="AutoDeployController as autoDeploy">
<div class="container-fluid">

<div class="row">
    <div class="col-md-2">
        <input type="text" class="form-control" ng-model="autoDeploy.gitRelease" placeholder="MM-DD-YYYY">
    </div>
</div>

<div class="row">
    <fieldset class="col-md-2" style="margin-bottom: 10px" ng-repeat="item in autoDeploy.tibcoCopyJobs track by $index">
        <legend>Copy</legend>

        <input type="text" class="form-control" placeholder="Source Server..." ng-model="item.sourceServer">
        <br/>
        <input type="text" class="form-control" placeholder="Source Path..." ng-model="item.sourcePath">
        <br/>
        <input type="text" class="form-control" placeholder="Destination Server..." ng-model="item.destinationServer">
        <br/>
        <input type="text" class="form-control" placeholder="Destination Path..." ng-model="item.destinationPath">
    </fieldset>
</div>

<button ng-click="autoDeploy.btn_extractFormData()">extract</button>
<button ng-click="autoDeploy.btn_addNewTibcoCopy()">TIBCO copy</button>
</div>
</div>

I think you have explained why in your question. 我认为您已经解释了问题的原因。 Array is returned by reference, whereas string is just copied by value. 数组通过引用返回,而字符串仅通过值复制。 But I will try to make it a bit more clear. 但是,我将尝试使其更加清晰。

When you do 当你做

model.gitRelease = AutoDeployService.getGitRelease();

the model object will create property getRelease like this: 模型对象将创建属性getRelease,如下所示:

{getRelease: "", ... (more properties from the ctrl)}

so whatever you update in the view it will just update the getRelease in the controller. 因此,无论您在视图中进行什么更新,它都只会更新控制器中的getRelease。

One possible fix is like what Jags mentioned in the comment. 一种可能的解决方法就像Jags在评论中提到的那样。

Or you can make a reference to your service in the ctrl 或者您可以在ctrl中引用您的服务

var model = this;
model.autoDeployService = AutoDeployService;

In your view 在您看来

<input type="text" class="form-control" ng-model="autoDeploy.autoDeployService.gitRelease" placeholder="MM-DD-YYYY">

that should work. 那应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM