简体   繁体   English

如何通过诺言将参数传递给AngularJS中的函数

[英]How to pass parameter to function in AngularJS with promises

I wan to send parameter to service function. 我想将参数发送到服务功能。

getQuestions : function(stateCode) : questionResource.js getQuestions:function(stateCode):questionResource.js

stateCode is set in $scope from the response of dtoResource.rc1Step1DTO() 根据dtoResource.rc1Step1DTO()的响应在$ scope中设置stateCode

angular
    .module('autoQuote')
    //Do initalization on page load
    .run(['$log', '$rootScope', '$state', 'dtoResource', 'questionResource', function($log, $rootScope, $state, dtoResource, questionResource) {
        $log.info('Post DTO on page load.');
        dtoResource.rc1Step1DTO()
            .then(questionResource.getQuestions)
            .then(function(questions) {
                $rootScope.questions = questions;
                console.log('Obtained questions. Assigned to rootscope');
            })
            .then(function() {
                console.log('This should be printed after the above methods are done     executing');
                console.log($rootScope);
            });

    }])

How to pass state code to the other function. 如何将状态代码传递给其他函数。 its position in scope is 它在范围上的位置是

$scope.postAutoQuoteObj.SessionInfo.StateCode

Below is the plunker for code http://plnkr.co/edit/Op1QDwUBECAosPUC7r3N?p=preview 以下是代码http://plnkr.co/edit/Op1QDwUBECAosPUC7r3N?p=preview的插件

your module autoQuote.run() will only be called once during your applications loading. 您的模块autoQuote.run()在应用程序加载期间将仅被调用一次。 So don't expect to have it execute again. 因此,不要期望它再次执行。

you are close to the right answer. 您接近正确的答案。 you just need to do some reorganization. 您只需要进行一些重组。 One tip I would offer is to worry about creating a directive later, first get the controllers, routes working first. 我要提供的一个技巧是担心稍后创建指令,首先获取控制器,首先运行路由。 You have created an excellent framework to start. 您已经创建了一个很好的框架来开始。 you are using ui-router. 您正在使用ui-router。 this means that you can tell ui-router what controller to execute when a state is requested. 这意味着您可以告诉ui-router在请求状态时执行哪个控制器。 so the life cycle for your application is as follows 因此您的应用程序的生命周期如下

module.run //any code in here first. module.run //首先在这里输入任何代码。 only on intial load module.config //in your case app.config which creates your states. 仅在初始加载module.config // //用于创建状态的app.config中。

then depending what state you are viewing based on the url; 然后根据网址查看您要查看的状态; the controller for that state (or view) will be executed 该状态(或视图)的控制器将被执行

so if you add another state config for / you can bind that state to your autoQuoteCtrl. 因此,如果为/添加另一个状态配置,则可以将该状态绑定到autoQuoteCtrl。

additionally, there are a number of other issues, such as attempting to use ng-model and value in an input element. 此外,还有许多其他问题,例如尝试在输入元素中使用ng-model和value。 this is not correct. 这是不正确的。

you are not implementing your ui-views. 您没有实现用户界面视图。 this will mean your controllers will never execute. 这将意味着您的控制器将永远不会执行。 technically, it your case autoQuoteCtrl would be executed but only because you forced it onto the page using ng-controller, which is what ui-router is designed to avoid. 从技术上讲,您的情况下的autoQuoteCtrl将被执行,但这仅是因为您使用ng-controller强制将其强制到页面上,而这正是ui-router旨在避免的。

which I think is why you ended up placing your code in .run(). 我认为这就是为什么您最终将代码放入.run()的原因。 This was the only place you found your applicaton would "run" the code on page load which is false, it was only running because your app loaded. 这是您发现applicaton会在页面加载时“运行”代码的唯一位置,这是错误的,它只是在运行,因为您的应用已加载。 Sorry if I'm being redundant. 对不起,如果我多余的话。 But it is an important point. 但这是重要的一点。

Also, you are trying to access your json data by passing statecode to your resource. 另外,您尝试通过将状态代码传递到资源来访问json数据。 that is not how $resource works. 这不是$ resource的工作原理。 you pass the name of the resource, and it will return everything at that resource. 您传递资源的名称,它将返回该资源的所有内容。 then you filter the code after the promise is returned. 然后您可以在返回承诺后过滤代码。 you only have one resource. 您只有一种资源。 that is "CA.json" so therefore you should be able to get away with only one line in your resource service and only need one resource service. 这就是“ CA.json”,因此您应该只能在资源服务中只使用一行,而只需要一项资源服务。 You will need additional non-resource services that can do the heaving lifting of filtering out the correct data from the JSON. 您将需要其他非资源服务,这些服务可以帮助您从JSON筛选出正确的数据。 typically this is why an app would call out to different resources, so that each one returns the model needed, not a single monolithic resource. 通常,这就是为什么应用会调用不同的资源,以便每个应用返回所需模型的原因,而不是单个整体资源的原因。

and you should never ever ever be using document.getElementById() you are using angular, so you do not interact with the dom directly 而且您永远都不要使用document.getElementById()来使用角度,因此不要直接与dom交互

finally, here is a much much revised plunkr to help guide you in the right direction. 最后,这里有一个经过大量修改的插头,可以帮助您正确地引导您。

http://plnkr.co/edit/dUJm01uu7RnhltC2pLLb?p=preview http://plnkr.co/edit/dUJm01uu7RnhltC2pLLb?p=预览

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

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