Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

javascript - 你可以在创建时将参数传递给AngularJS控制器吗?(Can you pass parameters to an AngularJS controller on creation?)

I have a controller responsible for communicating with an API to update properties of a user, name, email, etc. Each user has an 'id' which is passed from the server when the profile page is viewed.

(我有一个控制器负责与API通信以更新用户的属性,名称,电子邮件等。每个用户都有一个'id' ,在查看个人资料页面时从服务器传递。)

I would like to pass this value to the AngularJS controller so it knows what the API entry point is for the current user.

(我想将此值传递给AngularJS控制器,以便它知道当前用户的API入口点是什么。)

I've tried passing the value in ng-controller .

(我试过传递ng-controller的值。)

For example:

(例如:)

function UserCtrl(id, $scope, $filter) {

$scope.connection = $resource('api.com/user/' + id)

and in the HTML

(并在HTML中)

<body ng-controller="UserCtrl({% id %})">

where {% id %} print the id sent from the server.

(其中{% id %}打印从服务器发送的id。)

but I get errors.

(但我得到错误。)

What is the correct way to pass a value into a controller on its creation?

(在创建时将值传递给控制器??的正确方法是什么?)

  ask by nickponline translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Notes:

(笔记:)

This answer is old.

(这个答案很老了。)

This is just a proof of concept on how the desired outcome can be achieved.

(这只是关于如何实现预期结果的概念证明。)

However, it may not be the best solution as per some comments below.

(但是,根据下面的一些评论,它可能不是最佳解决方案。)

I don't have any documentation to support or reject the following approach.

(我没有任何文档支持或拒绝以下方法。)

Please refer to some of the comments below for further discussion on this topic.

(有关此主题的进一步讨论,请参阅以下一些评论。)

Original Answer:

(原答案:)

I answered this to Yes you absolutely can do so using ng-init and a simple init function.

(我回答这个是你绝对可以使用ng-init和一个简单的init函数。)

Here is the example of it on plunker

(这是它在plunker上的例子)

HTML

(HTML)

<!DOCTYPE html>
<html ng-app="angularjs-starter">
  <head lang="en">
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
    <script src="app.js"></script>
  </head>  
  <body ng-controller="MainCtrl" ng-init="init('James Bond','007')">
    <h1>I am  {{name}} {{id}}</h1>
  </body>
</html>

JavaScript

(JavaScript的)

var app = angular.module('angularjs-starter', []);

app.controller('MainCtrl', function($scope) {

  $scope.init = function(name, id)
  {
    //This function is sort of private constructor for controller
    $scope.id = id;
    $scope.name = name; 
    //Based on passed argument you can make a call to resource
    //and initialize more objects
    //$resource.getMeBond(007)
  };


});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...