angular.js是否可以在每个控制器中调用函数?

时间:2022-03-21 13:00:40

Hello im new to angular and I have a question that gave me some trouble, with researching and trying to make it work. Same task in knockout.js gave me no problems so I was wondering if it's possible in angular too.

您好我刚接触角度,我有一个问题给了我一些麻烦,研究并试图让它工作。 knockout.js中的相同任务没有给我带来任何问题,所以我想知道它是否有可能也是有角度的。

So I have:

所以我有:

<td ng-repeat="element in elements" ng-model="Element">`
<element-template style="position: static" ng-model="Element" el="element">`
</element-template>

and every repetition is giving me one controller to the html template looking something like this

并且每次重复都给我一个控制器到html模板看起来像这样

<div ng-model="el" class="element">
</div>

And the whole thing gets the data from pageController.controller[...] that controlls the array in which there is a data for elements. Every element has its own controller which look something like this:

整个事情从pageController.controller [...]获取数据,控制数据元素的数组。每个元素都有自己的控制器,看起来像这样:

pageController.directive('Element') {
  var controller = ['$scope', [...] , function ($scope, [...])
  {
    //code
    $scope.save() = function(){
        console.log("save successful");
    }
  }
}

And now the question: Is there a way for the pageController to make every element's controller to execute save() of the element?

现在的问题是:是否有一种方法让pageController使每个元素的控制器执行元素的save()?

In knockout I would just subscribe a function to the change of some observable in the element controller and in there just call the function but I couldn't find a way to do same thing in angular. Thanks in advance and sorry if the question is repeated or too trivial.

在淘汰赛中,我只是订阅一个函数来改变元素控制器中的一些observable并且在那里只是调用函数但是我找不到一种方法来在角度中做同样的事情。如果问题重复或过于微不足道,请提前致谢并表示歉意。

1 个解决方案

#1


0  

Ok, not sure I am following what you need, maybe a plunkr with a sample of your code would help. But if you are repeating an ng-model directive you can use the $index property to distinguish between your elements.

好的,不确定我是否按照你的需要,也许你的代码示例的一个plunkr会有所帮助。但是,如果要重复ng-model指令,可以使用$ index属性来区分元素。

So instead of just el, do this to bind to an array $scope.el = []; in your page controller:

所以不要只是el,而是绑定到数组$ scope.el = [];在您的页面控制器中:

<td ng-repeat="element in elements">
  <input type="text" ng-model="el[$index]" />
</td>

Also using ng-model in a div doesn't do anything, it only makes sense inside a input to double bind the value. I think you need to do some tutorials on the subject, it seems like you are unaware of how things are scoped and how ng-repeat works.

在div中使用ng-model也没有做任何事情,只有在输入中才能对值进行双重绑定才有意义。我认为你需要做一些关于这个主题的教程,看起来你不知道事情的范围是什么以及ng-repeat如何运作。

if are trying to access the objects within the elements array, for example you have an array of objects called elements:

如果试图访问elements数组中的对象,例如你有一个名为elements的对象数组:

$scope.elements = [
   { title: "one"},
   { title: "two"},
   { title: "three"},
];

You would then do this in your ng-repeat:

然后,您将在ng-repeat中执行此操作:

<td ng-repeat="element in elements">
  <input type="text" ng-model="element.title" />
</td>

#1


0  

Ok, not sure I am following what you need, maybe a plunkr with a sample of your code would help. But if you are repeating an ng-model directive you can use the $index property to distinguish between your elements.

好的,不确定我是否按照你的需要,也许你的代码示例的一个plunkr会有所帮助。但是,如果要重复ng-model指令,可以使用$ index属性来区分元素。

So instead of just el, do this to bind to an array $scope.el = []; in your page controller:

所以不要只是el,而是绑定到数组$ scope.el = [];在您的页面控制器中:

<td ng-repeat="element in elements">
  <input type="text" ng-model="el[$index]" />
</td>

Also using ng-model in a div doesn't do anything, it only makes sense inside a input to double bind the value. I think you need to do some tutorials on the subject, it seems like you are unaware of how things are scoped and how ng-repeat works.

在div中使用ng-model也没有做任何事情,只有在输入中才能对值进行双重绑定才有意义。我认为你需要做一些关于这个主题的教程,看起来你不知道事情的范围是什么以及ng-repeat如何运作。

if are trying to access the objects within the elements array, for example you have an array of objects called elements:

如果试图访问elements数组中的对象,例如你有一个名为elements的对象数组:

$scope.elements = [
   { title: "one"},
   { title: "two"},
   { title: "three"},
];

You would then do this in your ng-repeat:

然后,您将在ng-repeat中执行此操作:

<td ng-repeat="element in elements">
  <input type="text" ng-model="element.title" />
</td>