简体   繁体   English

无法从angularjs应用程序中的javascript函数返回数组列表

[英]unable to return array list from the javascript function in angularjs app

This is my link in mvc view and it is redirecting to details view by invoking function call. 这是我在mvc视图中的链接,它通过调用函数调用重定向到详细信息视图。 pls see below. 请参阅下面。

<a href="/Home/Details" class="label label-primary"  ng-click="addTask(c)">View Details</a>

TasksController.js TasksController.js

var lines =[]
$scope.addTask= function(c)
{
    lines.push(c);
    return lines;
}

alert(lines);

the return value (lines) is becoming null always for some reason. 由于某些原因,返回值(行)总是变为空。 I am unable to figure it out. 我无法弄清楚。 What I am trying to achieve is , 我要达到的目标是

I am passing the details from one view to another view in angularjs app. 我正在将详细信息从一个视图传递到angularjs应用程序中的另一个视图。


EDITED BELOW (latest) 下方编辑(最新)

I am getting totally crazy and unable to acccomplish this. 我快疯了,无法完成这项工作。

Index.cshtml Index.cshtml

    <a href="/Home/Details" class="label label-primary" ng-controller="TasksController" ng-model="c" ng-click="addTask(c)">View Details</a>

(c is my object which I am trying to pass it to the next page) (c是我试图将其传递到下一页的对象)

TaskController.js TaskController.js

app.controller("TasksController", ['$scope' ,'TasksService', function ($scope, TasksService) {
$scope.addTask = function (currObj) {
    var promiseAddTsk = TasksService.addProduct(currObj);
    $scope.products = promiseAddTsk[0].Name;
    console.log($scope.products);
};

}]); }]);

TasksService.js TasksService.js

 var productList = [];

this.addProduct = function (newObj) {
    productList.push(newObj);
    return productList;
};

Details.cshtml Details.cshtml

<div ng-app="TasksModule" ng-controller="TasksController">
<div>
    {{products}}
    <span ng-model="products">{{products}}</span>
</div>

I can see the "Name" in scope.Products but it doesn't get bind in details.cshtml. 我可以在scope.Products中看到“名称”,但是在details.cshtml中没有绑定。

I'd suggest you create a service. 我建议您创建一个服务。 You'd then need to inject the service into each controller and use the build in $q service to return the promise. 然后,您需要将服务注入每个控制器,并使用$ q内置服务返回承诺。 The service would look something like this 服务看起来像这样

.factory('Task', function($q) {
var lines = [];

 return {
addTask: function(c) {
    var deferred = $q.defer();
    lines.push(c);
    deferred.resolve(lines);
    return deferred.promise;
}
  };

});

and then you'd inject and use the service inside your controller like this .. 然后您就可以像这样在控制器内部注入和使用服务了。

.controller('TasksController', function($scope, Task) {
$scope.addTask = function(c){
    var lines = Task.addTask(c);
    alert(lines);
}
});

I believe your are missing ng-model in your HTML. 我相信您在HTML中缺少ng-model。

It should be: 它应该是:

<a href="/Home/Details" class="label label-primary"  ng-click="addTask(c)" ng-model="c">View Details</a>

If that is where the task is being set. 如果那是设置任务的地方。 If it is set in a form, then the ng-model needs to be set on the elements that sets the task, such as an input field. 如果以表格形式设置,则需要在设置任务的元素(例如输入字段)上设置ng-model。

The ng-model="c" should go wherever the task is being set on the UI. ng-model =“ c”应该放在用户界面上设置任务的任何地方。 Also for the time begin, try placing logic in one controller and separate out afterwords after proof of concept. 同样,在开始时,请尝试将逻辑放在一个控制器中,并在概念验证后分离出后言。

<div ng-app="TasksModule" ng-controller="TasksController">

     <a href="/Home/Details" class="label label-primary"  ng-click="addTask(c)" ng-model="c">View Details</a>

     <div>
        {{products}}
        <span ng-model="products">{{products}}</span>
     </div>
</div>

Then "c" or whatever you decide to call it would be passed from that element to the function addRask(c) as a parameter. 然后,“ c”或您决定调用的任何内容将从该元素作为参数传递给函数addRask(c)。

You could also inject $log into your factory and $log.debug the variable to see to what it is being set via the console. 您还可以将$ log注入工厂,然后将$ log.debug变量注入到控制台中,以查看其设置。

http://www.w3schools.com/angular/angular_directives.asp https://docs.angularjs.org/api/ng/directive/ngModel http://www.w3schools.com/angular/angular_directives.asp https://docs.angularjs.org/api/ng/directive/ngModel

Edit: make sure ng-click="addTask( product )" is set correctly, the name of the variable you are passing from the UI to the angular controller must be the same as the name of the model. 编辑:确保正确设置了ng-click =“ addTask( product )”,从UI传递到角度控制器的变量名称必须与模型名称相同。 You might have to move ng-click="addTask(product)" from the tag to a separate button and keep the tag dedicated to one task only. 您可能需要将ng-click =“ addTask(product)”从标记移至单独的按钮,并使标记仅专用于一项任务。

One thing of note is that you are using two different controllers - Task and Details. 需要注意的一件事是您正在使用两个不同的控制器-任务和详细信息。 A service for sharing data between controllers will work but for testing purposes, use the same controller in Index.cshtml as you do in Details.cshtml. 可以在控制器之间共享数据的服务可以使用,但是出于测试目的,请在Index.cshtml中使用与在Details.cshtml中相同的控制器。

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

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